tags:

views:

96

answers:

2

I coppied and pasted this example, and it seems to fail. Why is MethodBase null?

http://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo.isout.aspx

edit: here is a link to my code: http://img689.imageshack.us/img689/3453/94123952.png

Let me know where my copy & paste is wrong.

here is the code for those that cant view the image:

#region

using System;
using System.Reflection;

#endregion

namespace ConsoleApp
{
class parminfo
{
    public static void mymethod(
       int int1m, out string str2m, ref string str3m)
    {
        str2m = "in mymethod";
    }

    public static int Main(string[] args)
    {
        Console.WriteLine("\nReflection.Parameterinfo");

        //Get the ParameterInfo parameter of a function.

        //Get the type.
        Type Mytype = Type.GetType("parminfo");

        //Get and display the method.
        MethodBase Mymethodbase = Mytype.GetMethod("mymethod");
        Console.Write("\nMymethodbase = " + Mymethodbase);

        //Get the ParameterInfo array.
        ParameterInfo[] Myarray = Mymethodbase.GetParameters();

        //Get and display the IsOut of each parameter.
        foreach (ParameterInfo Myparam in Myarray)
        {
            Console.Write("\nFor parameter # " + Myparam.Position
               + ", the IsOut is - " + Myparam.IsOut);
        }
        return 0;
    }
}

}
+2  A: 

I copied and pasted the linked code and received the following output:

Reflection.Parameterinfo

Mymethodbase = Void mymethod(Int32, System.String ByRef, System.String ByRef)
For parameter # 0, the IsOut is - False
For parameter # 1, the IsOut is - True
For parameter # 2, the IsOut is - FalsePress any key to continue . . .

You clearly copied and pasted the code and made some changes that rendered the code incorrect. Copy and paste again but make no changes and execute the code. Let us know if that succeeds. Then, if you're trying to make changes and are receiving fails, let us know what the changes that you made are and we can help diagnose the problem.

Note: I am assuming that you meant the C# code as you tagged this C#. I did not test the VB.NET code.

Aside: Why can't Microsoft follow its own naming conventions in its sample code?

Jason
ive added a link to a screenshot of my code.
Mike_G
I can't look at ImageShack at work. Second, I would prefer just a paste of your code over an image.
Jason
code has been pasted
Mike_G
About your side note about naming convetions. This particular piece code was written for .NET 1.1. It is simply old and has not been updated according to conventions. You are welcome to send feedback on this topic. As far as I can see, nobody ever complained about it, that's why the writer left the code as it was
Alexandra Rusina
+3  A: 

Your problem is this code:

Type.GetType("parminfo")

This will try to find a type with a fully qualified name parminfo, but there isn't one such. Your class is declared in a namespace, and therefore its fully qualified name is ConsoleApp.parminfo.

Better yet, just use typeof(parminfo).

Pavel Minaev