简体   繁体   English

更改.Net中序列化的类的名称?

[英]Change the name of the class being serialized in .Net?

I can map incoming class names by using a SerializationBinder and overriding the BindToType method, but I found no way of changing the name of the class in the serialization process. 我可以使用SerializationBinder映射传入的类名并覆盖BindToType方法,但我发现无法在序列化过程中更改类的名称。 Is it possible at all?? 有可能吗?

EDIT: 编辑:

I am referring to the serialization using the System.Runtime.Serialization , and not the System.Xml.Serialization . 我指的是使用System.Runtime.Serialization的序列化,而不是System.Xml.Serialization

Thanks! 谢谢!

I'm not sure if I follow you, but you could use XmlTypeAttribute. 我不确定我是否关注你,但你可以使用XmlTypeAttribute。 You can then easily retrieve its values through reflection. 然后,您可以通过反射轻松检索其值。

[XmlType(Namespace = "myNamespaceThatWontChange",
TypeName = "myClassThatWontChange")]
public class Person
{
   public string Name;
}

Check this out: 看一下这个:

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmltypeattribute%28VS.100%29.aspx http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmltypeattribute%28VS.100%29.aspx

I found out that I can use the SerializationInfo object that comes in the GetObjectData function, and change the AssemblyName and FullTypeName properties, so that when I deserialize I can use a SerializationBinder to map the custom assembly and type-name back to a valid type. 我发现我可以使用GetObjectData函数中的SerializationInfo对象,并更改AssemblyNameFullTypeName属性,这样当我反SerializationBinder ,我可以使用SerializationBinder将自定义程序集和类型名称映射回有效类型。 Here is a semple: 这是一个semple:

Serializable class: 可序列化类:

[Serializable]
class MyCustomClass : ISerializable
{
    string _field;
    void MyCustomClass(SerializationInfo info, StreamingContext context)
    {
        this._field = info.GetString("PropertyName");
    }
    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AssemblyName = "MyCustomAssemblyIdentifier";
        info.FullTypeName = "MyCustomTypeIdentifier";
        info.AddValue("PropertyName", this._field);
    }
}

SerializationBinder: SerializationBinder:

public class MyBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        if (assemblyName == "MyCustomAssemblyIdentifier")
            if (typeName == "MyCustomTypeIdentifier")
                return typeof();
        return null;
    }
}

Serialization code: 序列化代码:

var fs = GetStream();
BinaryFormatter f = new BinaryFormatter();
f.Binder = new MyBinder();
var obj = (MyCustomClass)f.Deserialize(fs);

Deserialization code: 反序列化代码:

var fs = GetStream();
MyCustomClass obj = GetObjectToSerialize();
BinaryFormatter f = new BinaryFormatter();
f.Deserialize(fs, obj);

You can do it with attributes: 你可以用属性来做:

[System.Xml.Serialization.XmlRoot("xmlName")]
public Class ClassName
{
}

Look at using a surrogate + surrogate selector. 看看使用代理+代理选择器。 That together with a binder on the deserialization should do the trick. 这与反序列化的绑定一起应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM