我正在通過該值將一個參數/對象傳遞給函數「RefValTest(object oIn)」,但在函數調用結束之後,由該值傳遞的對象將發生更改,這種情況不會發生,對吧?在c#中傳遞的參數不能如預期那樣工作?使用內部函數反射
class MilanTest
{
public int SomeValue { get; set; }
}
class PR
{
public static object RefValTest(object oIn)
{
PropertyInfo tProperty = oIn.GetType().GetProperty("SomeValue");
tProperty.SetValue(oIn, 5, null);
return null;
}
}
主某處:
MilanTest x = new MilanTest();
//here is by compiler default x.Somevalue == 0
PR.RefValTest(x);
//here is by compiler default x.Somevalue == 5
它是如何可能的呼叫後, 「PR.RefValTest(X)」 對象x.SomeValue的值被改變?
Br, 米蘭。
通過值傳遞的對象沒有改變,只改變了它的一個屬性。 – tafa 2010-03-03 11:48:26
@tafa:* object *不會被值傳遞;一個*引用*是按值傳遞的。 – 2010-03-03 11:49:30