ISerializable.GetObjectData(SerializationInfo,StreamingContext)

时间:2013-09-25 15:09:11

标签: c# .net serialization interface

我正在网上寻找一个关于如何使用此接口的StreamingContext参数的实际示例,但我找不到任何东西。甚至MS Site上的文档也只是顺便提到它。有人可以演示如何以及何时使用它?

1 个答案:

答案 0 :(得分:-1)

它在BCL中使用,你实际上并不需要这个StreamingContext,因为实现是隐藏的。您可以反汇编BCL程序集以查看StreamingContext(ildsam mscorlib.dll)

StreamingContext的使用示例(它用作构造函数参数):

  .method assembly hidebysig instance void 
          CompleteSerialization(object serializedObject,
                                class System.Runtime.Serialization.SerializationInfo info,
                                valuetype System.Runtime.Serialization.StreamingContext context) cil managed
  {
    .custom instance void System.Security.SecurityCriticalAttribute::.ctor() = ( 01 00 00 00 ) 
    // Размер кода:       88 (0x58)
    .maxstack  4
    .locals init (class System.EventHandler`1<class System.Runtime.Serialization.SafeSerializationEventArgs> V_0,
             class System.Runtime.Serialization.SafeSerializationEventArgs V_1)
    IL_0000:  ldarg.0
    IL_0001:  ldnull
    IL_0002:  stfld      class System.Collections.Generic.IList`1<object> System.Runtime.Serialization.SafeSerializationManager::m_serializedStates
    IL_0007:  ldarg.0
    IL_0008:  ldfld      class System.EventHandler`1<class System.Runtime.Serialization.SafeSerializationEventArgs> System.Runtime.Serialization.SafeSerializationManager::SerializeObjectState
    IL_000d:  stloc.0
    IL_000e:  ldloc.0
    IL_000f:  brfalse.s  IL_0057

    IL_0011:  ldarg.3
    IL_0012:  newobj     instance void System.Runtime.Serialization.SafeSerializationEventArgs::.ctor(valuetype System.Runtime.Serialization.StreamingContext)
    IL_0017:  stloc.1
    IL_0018:  ldloc.0
    IL_0019:  ldarg.1
    IL_001a:  ldloc.1
    IL_001b:  callvirt   instance void class System.EventHandler`1<class System.Runtime.Serialization.SafeSerializationEventArgs>::Invoke(object,
                                                                                                                                          !0)
    IL_0020:  ldarg.0
    IL_0021:  ldloc.1
    IL_0022:  callvirt   instance class System.Collections.Generic.IList`1<object> System.Runtime.Serialization.SafeSerializationEventArgs::get_SerializedStates()
    IL_0027:  stfld      class System.Collections.Generic.IList`1<object> System.Runtime.Serialization.SafeSerializationManager::m_serializedStates
    IL_002c:  ldarg.2
    IL_002d:  ldstr      "CLR_SafeSerializationManager_RealType"
    IL_0032:  ldarg.1
    IL_0033:  callvirt   instance class System.Type System.Object::GetType()
    IL_0038:  ldtoken    System.RuntimeType
    IL_003d:  call       class System.Type System.Type::GetTypeFromHandle(valuetype System.RuntimeTypeHandle)
    IL_0042:  callvirt   instance void System.Runtime.Serialization.SerializationInfo::AddValue(string,
                                                                                                object,
                                                                                                class System.Type)
    IL_0047:  ldarg.2
    IL_0048:  ldtoken    System.Runtime.Serialization.SafeSerializationManager
    IL_004d:  call       class System.Type System.Type::GetTypeFromHandle(valuetype System.RuntimeTypeHandle)
    IL_0052:  callvirt   instance void System.Runtime.Serialization.SerializationInfo::SetType(class System.Type)
    IL_0057:  ret
  } // end of method SafeSerializationManager::CompleteSerialization