我有以下類:如何在C#中創建通用的克隆工廠方法?
public abstract class Item
{
//...
}
public class Customer : Item
{
//...
}
public class Address : Item
{
//...
}
我希望能夠使用自定義反射類這樣創建確切克隆:
Customer customer = new Customer();
ItemReflector irCustomer = new ItemReflector(customer);
Customer customerClone = irCustomer.GetClone<Customer>();
,但我有用GetClone方法的語法出現問題:
public class ItemReflector
{
private Item item;
public ItemReflector(Item item)
{
this.item = item;
}
public Item GetClone<T>()
{
T clonedItem = new T();
//...manipulate the clonedItem with reflection...
return clonedItem;
}
}
我必須更改上述GetClone()方法才能使用它?
看看我的答案[爲什麼沒有ICloneable?](http://stackoverflow.com/questions/536349/why-no-icloneablet) –
TcKs
2009-12-15 11:10:38