IDataContractSurrogate.GetDeserializedObject(Object, Type) 메서드

정의

deserialization 중에 지정한 개체를 대체하는 개체를 반환합니다.

public:
 System::Object ^ GetDeserializedObject(System::Object ^ obj, Type ^ targetType);
public object GetDeserializedObject (object obj, Type targetType);
abstract member GetDeserializedObject : obj * Type -> obj
Public Function GetDeserializedObject (obj As Object, targetType As Type) As Object

매개 변수

obj
Object

역직렬화되어 대체될 개체입니다.

targetType
Type

대체된 개체가 할당될 Type입니다.

반환

Object

대체되어 역직렬화된 개체입니다. 이 개체는 DataContractSerializer에서 serialize할 수 있는 형식이어야 합니다. 예를 들어, DataContractAttribute 특성 또는 serializer가 인식하는 기타 메커니즘으로 표시해야 합니다.

예제

다음 예제에서는 GetDeserializedObject 메서드의 구현을 보여 줍니다.

public object GetDeserializedObject(Object obj , Type targetType)
{
    Console.WriteLine("GetDeserializedObject invoked");
    // This method is called on deserialization.
    // If PersonSurrogated is being deserialized...
    if (obj is PersonSurrogated)
        {
            //... use the XmlSerializer to do the actual deserialization.
            PersonSurrogated ps = (PersonSurrogated)obj;
            XmlSerializer xs = new XmlSerializer(typeof(Person));
            return (Person)xs.Deserialize(new StringReader(ps.xmlData));
        }
        return obj;
}
Public Function GetDeserializedObject(ByVal obj As Object, _
    ByVal targetType As Type) As Object Implements _
    IDataContractSurrogate.GetDeserializedObject
    Console.WriteLine("GetDeserializedObject invoked")
    ' This method is called on deserialization.
    ' If PersonSurrogated is being deserialized...
    If TypeOf obj Is PersonSurrogated Then
        Console.WriteLine(vbTab & "returning PersonSurrogated")
        '... use the XmlSerializer to do the actual deserialization.
        Dim ps As PersonSurrogated = CType(obj, PersonSurrogated)
        Dim xs As New XmlSerializer(GetType(Person))
        Return CType(xs.Deserialize(New StringReader(ps.xmlData)), Person)
    End If
    Return obj

End Function

설명

간단한 구현에서 if...를 사용합니다. 다음... 값이 서로게이트 형식인지 여부를 obj 테스트하기 위한 컨트롤 구조체입니다. 그렇다면 필요에 따라 변환하고 대체된 개체를 반환합니다. 대체된 개체는 새 인스턴스 또는 동일한 obj 인스턴스일 수 있습니다.

적용 대상