ObjRef.GetObjectData(SerializationInfo, StreamingContext) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
用序列化当前 SerializationInfo 实例所需的数据来填充指定的 ObjRef。
public:
virtual void GetObjectData(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
public virtual void GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Security.SecurityCritical]
public virtual void GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
abstract member GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
override this.GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
[<System.Security.SecurityCritical>]
abstract member GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
override this.GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
Public Overridable Sub GetObjectData (info As SerializationInfo, context As StreamingContext)
参数
- info
- SerializationInfo
要填充数据的 SerializationInfo。
- context
- StreamingContext
有关序列化的源或目标的上下文信息。
实现
- 属性
例外
info
参数为 null
。
直接调用方没有序列化格式化程序权限。
示例
// a custom ObjRef class that outputs its status
[System::Security::Permissions::SecurityPermissionAttribute(
System::Security::Permissions::SecurityAction::Demand,
Flags=System::Security::Permissions::SecurityPermissionFlag::SerializationFormatter)]
[System::Security::Permissions::SecurityPermissionAttribute
(System::Security::Permissions::SecurityAction::Demand,
Flags=System::Security::Permissions::SecurityPermissionFlag::Infrastructure)]
[System::Security::Permissions::SecurityPermissionAttribute
(System::Security::Permissions::SecurityAction::InheritanceDemand,
Flags=System::Security::Permissions::SecurityPermissionFlag::Infrastructure)]
public ref class MyObjRef: public ObjRef
{
private:
// only instantiate via marshaling or deserialization
MyObjRef(){}
public:
MyObjRef( MarshalByRefObject^ o, Type^ t )
: ObjRef( o, t )
{
Console::WriteLine( "Created MyObjRef." );
ORDump();
}
MyObjRef( SerializationInfo^ i, StreamingContext c )
: ObjRef( i, c )
{
Console::WriteLine( "Deserialized MyObjRef." );
}
virtual void GetObjectData( SerializationInfo^ s, StreamingContext c ) override
{
// After calling the base method, change the type from ObjRef to MyObjRef
ObjRef::GetObjectData( s, c );
s->SetType( GetType() );
Console::WriteLine( "Serialized MyObjRef." );
}
virtual Object^ GetRealObject( StreamingContext context ) override
{
if ( IsFromThisAppDomain() || IsFromThisProcess() )
{
Console::WriteLine( "Returning actual Object* referenced by MyObjRef." );
return ObjRef::GetRealObject( context );
}
else
{
Console::WriteLine( "Returning proxy to remote Object*." );
return RemotingServices::Unmarshal( this );
}
}
void ORDump()
{
Console::WriteLine( " --- Reporting MyObjRef Info --- " );
Console::WriteLine( "Reference to {0}.", TypeInfo->TypeName );
Console::WriteLine( "URI is {0}.", URI );
Console::WriteLine( "\nWriting EnvoyInfo: " );
if ( EnvoyInfo != nullptr )
{
IMessageSink^ EISinks = EnvoyInfo->EnvoySinks;
while ( EISinks != nullptr )
{
Console::WriteLine( "\tSink: {0}", EISinks );
EISinks = EISinks->NextSink;
}
}
else
Console::WriteLine( "\t {no sinks}" );
Console::WriteLine( "\nWriting ChannelInfo: " );
for ( int i = 0; i < ChannelInfo->ChannelData->Length; i++ )
Console::WriteLine( "\tChannel: {0}", ChannelInfo->ChannelData[ i ] );
Console::WriteLine( " ----------------------------- " );
}
};
// a class that uses MyObjRef
public ref class LocalObject: public MarshalByRefObject
{
public:
// overriding CreateObjRef will allow us to return a custom ObjRef
[System::Security::Permissions::SecurityPermissionAttribute
(System::Security::Permissions::SecurityAction::LinkDemand,
Flags=System::Security::Permissions::SecurityPermissionFlag::Infrastructure)]
virtual ObjRef^ CreateObjRef( Type^ t ) override
{
return gcnew MyObjRef( this,t );
}
};
// a custom ObjRef class that outputs its status
public class MyObjRef : ObjRef {
// only instantiate via marshaling or deserialization
private MyObjRef() { }
public MyObjRef(MarshalByRefObject o, Type t) : base(o, t) {
Console.WriteLine("Created MyObjRef.");
ORDump();
}
public MyObjRef(SerializationInfo i, StreamingContext c) : base(i, c) {
Console.WriteLine("Deserialized MyObjRef.");
}
public override void GetObjectData(SerializationInfo s, StreamingContext c) {
// After calling the base method, change the type from ObjRef to MyObjRef
base.GetObjectData(s, c);
s.SetType(GetType());
Console.WriteLine("Serialized MyObjRef.");
}
public override Object GetRealObject(StreamingContext context) {
if ( IsFromThisAppDomain() || IsFromThisProcess() ) {
Console.WriteLine("Returning actual object referenced by MyObjRef.");
return base.GetRealObject(context);
}
else {
Console.WriteLine("Returning proxy to remote object.");
return RemotingServices.Unmarshal(this);
}
}
public void ORDump() {
Console.WriteLine(" --- Reporting MyObjRef Info --- ");
Console.WriteLine("Reference to {0}.", TypeInfo.TypeName);
Console.WriteLine("URI is {0}.", URI);
Console.WriteLine("\nWriting EnvoyInfo: ");
if ( EnvoyInfo != null) {
IMessageSink EISinks = EnvoyInfo.EnvoySinks;
while (EISinks != null) {
Console.WriteLine("\tSink: " + EISinks.ToString());
EISinks = EISinks.NextSink;
}
}
else
{
Console.WriteLine("\t {no sinks}");
}
Console.WriteLine("\nWriting ChannelInfo: ");
for (int i = 0; i < ChannelInfo.ChannelData.Length; i++)
Console.WriteLine ("\tChannel: {0}", ChannelInfo.ChannelData[i]);
Console.WriteLine(" ----------------------------- ");
}
}
// a class that uses MyObjRef
public class LocalObject : MarshalByRefObject {
// overriding CreateObjRef will allow us to return a custom ObjRef
public override ObjRef CreateObjRef(Type t) {
return new MyObjRef(this, t);
}
}
注解
当前方法使用重新创建 ObjRef所需的所有数据填充提供的 SerializationInfo 。
当前方法是 的 GetObjectData实现。