假设 Automapper 是不可能的(正如@lazyberezovsky 在他的回答中指出的那样),您可以将其序列化为副本:
public object[] Copy(object obj) {
using (var memoryStream = new MemoryStream()) {
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, obj);
memoryStream.Position = 0;
return (object[])formatter.Deserialize(memoryStream);
}
}
[Serializable]
class testobj {
public string Name { get; set; }
}
class Program {
static object[] list = new object[] { new testobj() { Name = "TEST" } };
static void Main(string[] args) {
object[] clonedList = Copy(list);
(clonedList[0] as testobj).Name = "BLAH";
Console.WriteLine((list[0] as testobj).Name); // prints "TEST"
Console.WriteLine((clonedList[0] as testobj).Name); // prints "BLAH"
}
}
但请注意:这一切都非常低效......当然有更好的方法来做你想做的事情。