我只是好奇在 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");
}
}