繁体   English   中英

为什么反射上的此MSDN示例失败?

[英]Why does this MSDN example on Reflection fail?

我复制并粘贴了此示例,但它似乎失败了。 为什么MethodBase为null?

http://msdn.microsoft.com/zh-CN/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)

我复制并粘贴了链接的代码,并收到以下输出:

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 . . .

您清楚地复制并粘贴了代码,并进行了一些更改,使代码不正确。 再次复制并粘贴,但不做任何更改并执行代码。 让我们知道那是否成功。 然后,如果您尝试进行更改但接收失败,请告诉我们您所做的更改,我们将帮助您诊断问题。

注意:我假设您是在标记此C#时使用的是C#代码。 我没有测试VB.NET代码。

旁白:为什么Microsoft不能在其示例代码中遵循其自己的命名约定

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM