也許這是真正簡單的或違反所有的規則,或者我只是不知道它叫什麼,所以我不能找到它。替換堆上的對象?
無論如何,我希望能夠替換堆上的整個對象。我已經添加了一個小代碼示例來展示我想要做什麼以及如何做,但我只想知道是否有更優雅的方式?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BasicObjectTest
{
class Program
{
static void Main(string[] args)
{
List<Test> testList = new List<Test>
{
new Test {Value=1,NiceString="First" },
new Test {Value=2,NiceString="Second" },
new Test {Value=3,NiceString="Third" }
};
var replacementTestClass = new Test { Value = 2, NiceString = "NEW" };
EasyWay(testList, replacementTestClass);
var correctTestClass = testList.FirstOrDefault(x => x.Value == 2);
Console.WriteLine(correctTestClass.NiceString); //Expecting "Forth"
Console.ReadLine();
HardWay(testList, replacementTestClass);
correctTestClass = testList.FirstOrDefault(x => x.Value == 2);
Console.WriteLine(correctTestClass.NiceString);
Console.ReadLine();
}
private static void HardWay(List<Test> testList, Test replacementTestClass)
{
//This will work!
var secondTestClass = testList.FirstOrDefault(x => x.Value == 2);
CopyPropertiesUsingPropertyInfo(secondTestClass, replacementTestClass);
}
private static void CopyPropertiesUsingPropertyInfo(Test secondTestClass, Test replacementTestClass)
{
foreach(var pi in secondTestClass.GetType().GetProperties())
{
pi.SetValue(secondTestClass, pi.GetValue(replacementTestClass, null));
}
}
private static void EasyWay(List<Test> testList, Test replacementTestClass)
{
//This wont work, but I want it to!
var secondTestClass = testList.FirstOrDefault(x => x.Value == 2);
secondTestClass = replacementTestClass;
}
}
}
和我的測試對象
class Test
{
public int Value { get; set; }
public string NiceString { get; set; }
}
必須有這樣做的更優雅的方式? 我知道爲什麼第一個替代方法不起作用:我只是更改該變量的對象引用。
更新: 使用這種思想,我很長一段時間瞭解它,我現在認爲它會工作,但測試失敗。爲什麼?我沒有替換該對象,以便每個使用它的對象都應該使用新對象?見下面
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var main = new Main { Property = 1 };
var dependent = new Dependent(main);
void ChangeRef(ref Main Oldmain, Main newMain)
{
Oldmain = newMain;
}
ChangeRef(ref main, new Main { Property = 5 });
Assert.AreEqual(5,dependent.Main.Property);
}
}
public class Main
{
public int Property { get; set; }
}
public class Dependent
{
public Dependent(Main main)
{
Main = main;
}
public Main Main { get; set; }
}
我不知道關於傳遞對象作爲參考。我已經將它用於值類型。 – Erik83
@ Erik83引用類型也通過值***通過***。不同之處在於,該值實際上是指向虛擬內存中地址的引用,而不是像值類型那樣的實際字節。 –