2023-01-18

Is there a way to enforce pass-by-reference even if the parameter does not have "ref"? [closed]

Is there anything I can do, some hacking I suppose, inside a function, which has a pass-by-value parameter, so that it works as if there is a "ref" in front of the parameter?

Suppose I have this code:

class MyClass
{
    public string Name { get; set; }
}

static void PassByValue(MyClass myClass)
{
    myClass = new MyClass { Name ="Peter"};
}

static void PassByRef(ref MyClass myClass)
{
    myClass = new MyClass { Name ="Peter"};
}

static void Main()
{
    var myClass = new MyClass { Name = "John" };
    PassByValue(myClass);
    Console.WriteLine(myClass.Name); // It will print "John"

    myClass = new MyClass { Name = "John" };
    PassByRef(ref myClass);
    Console.WriteLine(myClass.Name); // It will print "Peter"
}

Is there any way that I can do inside the PassByValue function, without changing its signature, so that it behaves like the PassByRef function, and it prints "Peter"?

Why do I need to do this weird thingy?

We are developing a patented technology:

SkyBridge Proxy DLL

You can generate a proxy DLL from a normal (original) DLL. It has exactly the same public interface as the original DLL. Then you can give this proxy DLL to anyone in the world. They can write software to invoke that proxy DLL, and that proxy DLL will use SkyBridge InstantRemoting to remotely invoke the original DLL and get the data back. This way, in the eyes of the caller of the proxy DLL, it behaves exactly the same as the original DLL.

So, if one function in the original DLL has a pass-by-value MyClass as parameter, the proxy DLL must have the same. But the MyClass object passed into the proxy DLL won't be physically sent to the original DLL. Instead, it is serialized and sent to the original DLL via Internet. Then the modified MyClass is serialized and sent back. The proxy DLL then create an instance from the sent-back serialized data. It is no longer the same physical instance as what had been passed in. But I want the caller of the proxy DLL to get the changed instance of MyClass.



No comments:

Post a Comment