한다고 가정 나는 다음과 같은 한 종류 :기존 인스턴스에서 동일한 유형의 새 인스턴스를 만드는 방법은 무엇입니까? 코드의
foreach(var x in Mylist) //MyList is EntitySet
{
//......
}
내가 x의 유형을 알고 같은 유형의 또 다른 새로운 인스턴스와 같은 새로운 인스턴스에 X 클론 만들려면 :
foreach(var x in Mylist)
{
string tyoename = typeof(x).AssemblyQualifiedName; //get the type of x, but i got error here
//create instance of the type
//clone x data to new instance
}
으로 myList를 동적 데이터이므로, Mylist가 변경되면 x가 다른 유형이 될 수 있습니다. 이 요청을 구현하는 방법은 무엇입니까? 이처럼
public static class CloningExtensions
{
public static T Clone<T>(this T source)
{
// var dcs = new DataContractSerializer(typeof(T), null, int.MaxValue, false, true, null);
var dcs = new System.Runtime.Serialization
.DataContractSerializer(typeof(T));
using (var ms = new System.IO.MemoryStream())
{
dcs.WriteObject(ms, source);
ms.Seek(0, System.IO.SeekOrigin.Begin);
return (T)dcs.ReadObject(ms);
}
}
}
:
여기에 개체를 복제에 대한 토론과 유사 할 수 : http://stackoverflow.com/questions/78536/cloning-objects-in-c-sharp이 당신은, 일반적인 경우에 가능하지 않다 –
복제하려고하는 객체에 대해 몇 가지 가정을해야 할 것입니다 ... surfen의 대답은 객체가 직렬화 가능하다고 가정하는 한 꽤 좋습니다. – Yaur