2009-12-23 55 views
1

我複製和粘貼這個例子,它似乎失敗。爲什麼MethodBase爲空?爲什麼此反射中的MSDN示例失敗?

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; 
    } 
} 

} 
+2

發佈**您的**代碼。有時候人們會複製一些東西並改變一些東西,然後想知道爲什麼它不起作用。 – 2009-12-23 17:00:18

+0

粘貼完整的代碼,就像你與你一樣。我想,這可能是由於方法案例名稱不匹配(您已經定義方法的方式以及使用反射時的方式)。 – shahkalpesh 2009-12-23 17:01:39

回答

3

你的問題是這樣的代碼:

Type.GetType("parminfo") 

這將嘗試找到一個全名parminfo一個類型,但沒有這樣的。您的類在名稱空間中聲明,因此其完全限定名稱爲ConsoleApp.parminfo

更好的是,只需使用typeof(parminfo)

2

我複製並粘貼linked code並取得了以下的輸出:

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不能在其示例代碼中遵循自己的naming conventions

+0

ive添加了我的代碼截圖的鏈接。 – 2009-12-23 18:28:05

+0

我無法在工作中查看ImageShack。其次,我寧願只在圖片上粘貼代碼。 – jason 2009-12-23 18:32:35

+0

代碼已被粘貼 – 2009-12-23 18:35:58

相關問題