c# - 为什么这个关于反射的 MSDN 示例失败了?

标签 c# reflection

我复制并粘贴了这个例子,但似乎失败了。为什么 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/

相关文章:

scala - 列出所有可见的隐式

Java Reflection - 如何为从 One 和 Two 扩展的类设置值

c# - 为什么在尝试创建大小为 int.MaxValue 的 int 数组时出现 OutOfMemoryException

c# - Windows 手机 : Call an Async method in OnNavigatedTo

c# - 有没有办法根据初始枚举值使用不同的构造函数?

reflection - Lua - 反射 - 函数参数和文档字符串?

c# - 如何调用 Activator.CreateInstance,将方法作为构造函数参数传递?

c# - 有条件的或在 C# 中

c# - 从 SQL Server Compact 反序列化会引发 "There is an unclosed literal string."错误

c# - 如何在 C# 中通过字符串调用构造函数(即通过反射)?