Why does this MSDN example on Reflection fail?
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)
{
Consol开发者_JAVA技巧e.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;
}
}
}
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)
.
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?
精彩评论