我试图在silverligth 5中创建对象的副本,其中 IFormatters 和 IcCloanble 等接口不支持。*
我的对象是这样的:(请注意,这些对象是在反序列化 xml 时获得的):我尝试像这样复制:
[XmlRoot(ElementName = "component")]
public class Component
{
[XmlElement("attributes")]
public Attributes Attributes { get; set; }
[XmlIgnore]
public Attributes atrbtOrginal = new Attributes();
[XmlIgnore]
public Attributes atrbtCopy{ get; set; }
}
public Component()
{
atrbtCopy= atrbtOrginal ;
}
当然它不起作用,然后我在谷歌上搜索到了这个代码:
public static class ObjectCopier
{
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
}
And i thought of doing something liek this:
objectOrginal.Clone();.
但是silverligth5中的问题是:
Error 2 The type or namespace name 'BinaryFormatter' could not be found (are you missing a using directive or an assembly reference?)
Error 1 The type or namespace name 'IFormatter' could not be found (are you missing a using directive or an assembly reference?)
Silverlight 5 中是否有任何替代方案。请详细说明。非常感谢。