タイプに[DataContract]
属性を使用して、シリアル化可能にしています。いくつかのプロパティ/フィールドは、私がそのままシリアル化したくないstruct型のものですが、シリアル化には厳密にカスタム型を使いたいと思っています。シリアル化中に型に対して独自の変換を定義することはできますか?
どのようにタイプをシリアル化に置き換えるのですか?
タイプに[DataContract]
属性を使用して、シリアル化可能にしています。いくつかのプロパティ/フィールドは、私がそのままシリアル化したくないstruct型のものですが、シリアル化には厳密にカスタム型を使いたいと思っています。シリアル化中に型に対して独自の変換を定義することはできますか?
どのようにタイプをシリアル化に置き換えるのですか?
.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;
}
}
[データ契約代理人](https://msdn.microsoft.com/en-us/library/ms733064(v=vs.110).aspx)はどうですか? – stuartd
これは私が必要としているものです。答えにして、私はそれをマークします – Thraka
私はリンクオンリーの答えを出すのは好きではありませんが、助けてくれて嬉しいです。 – stuartd