2009-04-24 60 views

回答

16

對於參考參數(或在C#中輸出),反射會將新值複製到與原始參數相同位置的對象數組中。您可以訪問該值以查看更改後的參考。

public class Example { 
    public static void Foo(ref string name) { 
    name = "foo"; 
    } 
    public static void Test() { 
    var p = new object[1]; 
    var info = typeof(Example).GetMethod("Foo"); 
    info.Invoke(null, p); 
    var returned = (string)(p[0]); // will be "foo" 
    } 
} 
1

如果你打電話Type.GetMethod和使用的BindingFlag只是BindingFlags.Static,它不會找到你的方法。刪除標誌或添加BindingFlags.Public,它會找到靜態方法。

public Test { public static void TestMethod(int num, ref string str) { } } 

typeof(Test).GetMethod("TestMethod"); // works 
typeof(Test).GetMethod("TestMethod", BindingFlags.Static); // doesn't work 
typeof(Test).GetMethod("TestMethod", BindingFlags.Static 
            | BindingFlags.Public); // works 
+0

你說得對。謝謝。不是我原來的問題的來源,但仍然是一個問題。 – Deane 2009-04-24 18:11:41

相關問題