开发者

Argument passing in c# does not work as should ? Inside function used reflection

I am passing one parameter/object to the function "RefValTest(object oIn)" by the value but after function call is over object passed by the value is changed, that shoud not happened, right ?

    class MilanTest
    { 
        public int SomeValue { get; set; }
    }

    class PR
    {

      public static object RefValTest(object oIn)
      { 

        PropertyInfo tProperty = oIn.GetType().GetProperty("SomeV开发者_开发问答alue");
        tProperty.SetValue(oIn, 5, null);

        return null;
       }

     }

somewhere in the main:

        MilanTest x = new MilanTest ();
        //here is by compiler default x.Somevalue == 0

        PR.RefValTest(x);
        //here is by compiler default x.Somevalue == 5

How it is possible that after call of "PR.RefValTest(x)" value of object x.SomeValue is changed ?

Br, Milan.


The problem is that you're confusing what you're passing, and passing an object "by value" never quite works.

When you pass a reference type (anything derived from System.Object), what you're passing is a reference to that object - not the object itself. Essentially you're passing the memory address that the object's data is stored at. Whether you pass this by reference or by value, what happens inside the method is that the address is followed to your original object. Whether you pass a "pointer to the address" or "a copy of the address", the address itself doesn't change.

So, regardless of how you pass that type, you're going to find that the original object is what you're modifying. Your alternative, if you really need this functionality, is to create a clone of the object and pass in the clone instead (or make your method create the clone itself). That way the clone is modified instead of your original object.


Very simply: what's being passed into the method is a reference to the object. You're then changing the data within that object. So when you look at the object again afterwards, you can see the change.

You don't need to use reflection to see this working - here's a simpler version of your code:

class MilanTest
{ 
    public int SomeValue { get; set; }
}

class Test
{
    static void RefValTest(MilanTest x)
    { 
        x.SomeValue = 5;
    }

    static void Main()
    {
        MilanTest t = new MilanTest();
        RefValTest(t);
        Console.WriteLine(t.SomeValue); // Prints 5
    }
}

See my article on parameter passing for more details.


You cannot change the reference oIn but you can change its properties.

So inside your method, if you say

oIn = "SomeString";

on the outside you wouldn't see this.


Wrong. This should happen, whether you set the value directly or take the detour over reflection. This is what pass-by-reference means: you are interacting with the same object instance, not a copy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜