“类型”属性的数据契约序列化

本文关键字:数据 契约 序列化 属性 类型 | 更新日期: 2023-09-27 18:09:12

如何在我的DataContract属性类中有效地序列化类型为" type "的属性?我假设Type是一个不可序列化的Type(哇,这听起来很傻)。我相信有一种方法可以满足我的需求。基本上,我需要序列化工厂方法的类型名称以有效地构造,但我不想将其作为字符串公开,我想要一个type。

我知道有很多方法可以做到这一点,我很好奇目前已知的其他方法。

编辑:我刚刚意识到它可能是其他东西导致的,但这里是错误,下面我有类定义。

类型的系统。不需要使用数据合约名称"RuntimeType:http://schemas.datacontract.org/2004/07/System"的运行类型。考虑使用DataContractResolver或将任何未知静态类型添加到已知类型列表中—例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中。

[DataContract]
public class PlottingDeviceInfo : ObservableObject
{
    private string _deviceName;
    [DataMember]
    public string DeviceName
    {
        get
        {
            return _deviceName;
        }
        set
        {
            Set(() => DeviceName, ref _deviceName, value);
        }
    }
    private Type _deviceType;
    [DataMember]
    public Type DeviceType
    {
        get
        {
            return _deviceType;
        }
        set
        {
            Set(() => DeviceType, ref _deviceType, value);
        }
    }
    private DeviceSettingsInfo _settings;
    [DataMember]
    public DeviceSettingsInfo Settings
    {
        get
        {
            return _settings;
        }
        set
        {
            Set(() => Settings, ref _settings, value);
        }
    }
    private DeviceChannelInfo _channel;
    [DataMember]
    public DeviceChannelInfo Channel
    {
        get
        {
            return _channel;
        }
        set
        {
            Set(() => Channel, ref _channel, value);
        }
    }
    private DeviceCategory _deviceCategory;
    [IgnoreDataMember]
    public DeviceCategory DeviceCategory
    {
        get
        {
            return _deviceCategory;
        }
        set
        {
            Set(() => DeviceCategory, ref _deviceCategory, value);
        }
    }
}

这是基类,用于为视图模型消费添加可观察性。

[DataContract]
public class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    [IgnoreDataMember]
    protected PropertyChangedEventHandler PropertyChangedHandler
    {
        get
        {
            return PropertyChanged;
        }
    }
    [Conditional("DEBUG")]
    [DebuggerStepThrough]
    public void VerifyPropertyName(string propertyName)
    {
        var myType = this.GetType();
        if (!string.IsNullOrEmpty(propertyName)
            && myType.GetProperty(propertyName) == null)
        {
            throw new ArgumentException("Property not found", propertyName);
        }
    }
    protected virtual void RaisePropertyChanged(string propertyName)
    {
        VerifyPropertyName(propertyName);
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
    {
        if (propertyExpression == null)
        {
            return;
        }
        var handler = PropertyChanged;
        if (handler != null)
        {
            var body = propertyExpression.Body as MemberExpression;
            handler(this, new PropertyChangedEventArgs(body.Member.Name));
        }
    }
    protected void Set<T>(
        Expression<Func<T>> propertyExpression,
        ref T field,
        T newValue)
    {
        if (EqualityComparer<T>.Default.Equals(field, newValue))
        {
            return;
        }
        field = newValue;
        RaisePropertyChanged(propertyExpression);
    }
    protected void Set<T>(
        string propertyName,
        ref T field,
        T newValue)
    {
        if (EqualityComparer<T>.Default.Equals(field, newValue))
        {
            return;
        }
        field = newValue;
        RaisePropertyChanged(propertyName);
    }
}

“类型”属性的数据契约序列化

Type不能跨平台表示,因此没有内置表示。最好的方法是将其表示为字符串,即

public Type DeviceType { get; set; }
[DataMember(Name="DeviceType")]
private string DeviceTypeName {
    get { return DeviceType == null ? null : DeviceType.AssemblyQualifiedName; }
    set { DeviceType = value == null ? null : Type.GetType(value); }
}

在根类中添加KnownType属性,并传入System.RuntimeType作为类型。

在c#

,

[KnownType(typeof(System.RuntimeType))]

您还可以做的,特别是如果您不想更改PlottingDeviceInfo,是在DataContractSerializer的构造函数中传递IDataContractSurrogate。我所做的(不知道是否有更简单的方法)是定义

public class TypeWrapper
{
    public string TypeName;
}

,然后这样使用:

public class TypeReplacementSurrogate : IDataContractSurrogate
{
    public object GetCustomDataToExport(Type clrType, Type dataContractType)
    {
        return null;
    }
    public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType)
    {
        return null;
    }
    public Type GetDataContractType(Type type)
    {
        if(type == typeof(Type))
        {
            return typeof (TypeWrapper);
        }
        return type;
    }
    public object GetDeserializedObject(object obj, Type targetType)
    {
        var objAsTypeWrapper = obj as TypeWrapper;
        if (objAsTypeWrapper != null)
        {
            return Assembly.GetExecutingAssembly().GetType(objAsTypeWrapper.TypeName);
        }
        return obj;
    }

    public void GetKnownCustomDataTypes(System.Collections.ObjectModel.Collection<Type> customDataTypes)
    {
    }
    public object GetObjectToSerialize(object obj, Type targetType)
    {
        var objAsType = obj as Type;
        if (objAsType != null)
        {
            return new TypeWrapper() {TypeName = objAsType.FullName};
        }
        return obj;
    }
    public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
    {
        return null;
    }
    public System.CodeDom.CodeTypeDeclaration ProcessImportedType(
        System.CodeDom.CodeTypeDeclaration typeDeclaration, System.CodeDom.CodeCompileUnit compileUnit)
    {
        return null;
    }
}