2016-09-06 2 views
0

タイプに[DataContract]属性を使用して、シリアル化可能にしています。いくつかのプロパティ/フィールドは、私がそのままシリアル化したくないstruct型のものですが、シリアル化には厳密にカスタム型を使いたいと思っています。シリアル化中に型に対して独自の変換を定義することはできますか?

どのようにタイプをシリアル化に置き換えるのですか?

+2

[データ契約代理人](https://msdn.microsoft.com/en-us/library/ms733064(v=vs.110).aspx)はどうですか? – stuartd

+0

これは私が必要としているものです。答えにして、私はそれをマークします – Thraka

+0

私はリンクオンリーの答えを出すのは好きではありませんが、助けてくれて嬉しいです。 – stuartd

答えて

0

.NETで[DataContract]属性を使用している場合は、通常、DataContractSerializerを使用して型のシリアル化を行います。 DataContractSerializerには、DataContractSurrogateという名前の読み取り専用プロパティがあります。このプロパティはIDataContractSurrogateインターフェイスです。 (読み取り専用プロパティは、コンストラクタまたはDataContractSerializerSettingsタイプで設定できます)

このインターフェイスには、シリアル化中にあるタイプを別のタイプに変換するロジックが含まれています。インターフェイスによって提供される8つのメソッドがありますが、シリアル化および逆シリアル化中にある型を別の型に変換するには、実際には3を実装する必要があります。次に、型を代入してロジックを変換できるインターフェイスを実装するクラスの基本構造です。

internal class SerializerSurrogate : IDataContractSurrogate 
{ 
    /// <summary> 
    /// Identifies a type transformation. 
    /// </summary> 
    /// <param name="type">The type being inspected.</param> 
    /// <returns>The type to transform to. If the same type is used, transform does not happen.</returns> 
    public Type GetDataContractType(Type type) 
    { 
     if (type == typeof(sourceType)) 
      return typeof(targetType); 

     return type; 
    } 

    /// <summary> 
    /// During serialization, transfrom from sourceType to targetType. 
    /// </summary> 
    /// <param name="obj">The object instance.</param> 
    /// <param name="targetType">The source type.</param> 
    /// <returns>A new object instance based on targetType.</returns> 
    public object GetObjectToSerialize(object obj, Type targetType) 
    { 
     if (obj is sourceType) 
     { 
      // Create instance of targetType from the obj variable which is sourceType 
      // Set the properties of the targetType, based on the (sourceType)obj variable. 
      return new targetType(); 
     } 

     return obj; 
    } 

    /// <summary> 
    /// During deserialization, transform from targetType to sourceType. 
    /// </summary> 
    /// <param name="obj">The object instance.</param> 
    /// <param name="targetType">The target type.</param> 
    /// <returns>A new object instance based on sourceType.</returns> 
    public object GetDeserializedObject(object obj, Type targetType) 
    { 
     if (obj is targetType) 
     { 
      // Create instance of sourceType from the obj variable which is targetType 
      // Set the properties of the sourceType, based on the (targetType)obj variable. 
      return new sourceType(); 
     } 

     return obj; 
    } 

    public object GetCustomDataToExport(Type clrType, Type dataContractType) 
    { 
     throw new NotImplementedException(); 
    } 

    public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType) 
    { 
     throw new NotImplementedException(); 
    } 

    public void GetKnownCustomDataTypes(Collection<Type> customDataTypes) 
    { 
     throw new NotImplementedException(); 
    } 

    public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData) 
    { 
     return null; 
    } 

    public CodeTypeDeclaration ProcessImportedType(CodeTypeDeclaration typeDeclaration, CodeCompileUnit compileUnit) 
    { 
     return null; 
    } 
} 
関連する問題