DataContract сериализация свойства "Типа" - PullRequest
7 голосов
/ 20 сентября 2011

Как я могу эффективно сериализовать свойство типа "Тип" в моем атрибутивном классе DataContract? Я предполагаю, что Type - это не сериализуемый Type (вау, это звучит глупо). Я уверен, что есть способ сделать это, отвечающий моим потребностям. По сути, мне нужно сериализовать имя типа для эффективного метода фабрики, но я не хочу показывать его в виде строки, я хочу тип.

Я знаю, что есть несколько способов сделать это, мне любопытно, какие другие методы известны в настоящее время.

EDIT: Я только что понял, что это может быть чем-то другим, вызывающим это, но вот ошибка, и ниже у меня есть определение класса.

Тип 'System.RuntimeType' с именем контракта данных '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);
    }
}

Ответы [ 3 ]

10 голосов
/ 20 сентября 2011

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); }
}
0 голосов
/ 11 апреля 2013

То, что вы также можете сделать, особенно если вы не хотите изменять PlottingDeviceInfo, это передать IDataContractSurrogate в конструктор DataContractSerializer.Что я сделал (не знаю, есть ли более простой способ), так это определил

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;
    }
}
0 голосов
/ 20 сентября 2011

В корневой класс добавьте атрибут KnownType с System.RuntimeType, переданным как тип.

В C #,

[KnownType(typeof(System.RuntimeType))]
...