3

そのようなことがC#で可能かどうか、私はただ興味があります。なぜ誰もがそれをやりたがるのかはわかりませんが、それができれば興味深いです.

public class Test
{
    public string TestString { private set; get; }
    public Test(string val) { TestString = val; }
}

    public class IsItPossible
    {
        public void IsItPossible()
        {
            Test a = new Test("original");
            var b = a;
            //instead of assigning be to new object, I want to get where b is pointing and change the original object
            b = new Test("Changed"); // this will assign "b" to a new object", "a" will stay the same. We want to change "a" through "b"
            //now they will point to different things
            b.Equals(a); // will be false
            //what I'm curious about is getting where b is pointing and changing the object itself, not making just b to point to a new object
            //obviously, don't touch a, that's the whole point of this challenge

            b = a;
            //some magic function
            ReplaceOriginalObject(b, new Test("Changed"));
            if (a.TestString == "Changed" && a.Equals(b)) Console.WriteLine("Success");
        }
    }
4

1 に答える 1