我复制并粘贴了这个例子,但似乎失败了。为什么 MethodBase 为空?
http://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo.isout.aspx
编辑: 这是我的代码的链接: http://img689.imageshack.us/img689/3453/94123952.png
让我知道我的复制和粘贴错误的地方。
这里是那些无法查看图像的代码:
#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;
}
}
}
最佳答案
你的问题是这段代码:
Type.GetType("parminfo")
这将尝试找到具有完全限定名称 parminfo
的类型,但没有这样的类型。您的类在命名空间中声明,因此其完全限定名称为 ConsoleApp.parminfo
。
更好的是,只需使用 typeof(parminfo)
。
关于c# - 为什么这个关于反射的 MSDN 示例失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1953996/