Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 可串行化和同步_C#_Serialization_Synchronization - Fatal编程技术网

C# 可串行化和同步

C# 可串行化和同步,c#,serialization,synchronization,C#,Serialization,Synchronization,我用[Synchronization]属性创建了一个线程安全的小类,它还实现了ContextBoundObject接口。我想使这个类[可序列化]。代码已编译,但我收到一个 System.Runtime.Remoting.RemotingException: Remoting cannot find field '__identity' on type 'System.MarshalByRefObject' 例外。我知道用[Synchronization]标记的类是通过某种“远程”代理访问的,但

我用[Synchronization]属性创建了一个线程安全的小类,它还实现了ContextBoundObject接口。我想使这个类[可序列化]。代码已编译,但我收到一个

System.Runtime.Remoting.RemotingException: Remoting cannot find field '__identity' on type 'System.MarshalByRefObject'
例外。我知道用[Synchronization]标记的类是通过某种“远程”代理访问的,但我希望默认的序列化机制能够工作

这是一个小应用程序,该类只是一个内存序列(整数id)生成器,因此没有进行真正的远程处理。我想我可以通过其他方式(lock关键字)进行同步,但我想知道这个问题的标准解决方案是什么


更新:最后,为了使用基本序列化,我放弃了[Synchronization]属性。

将任何不应序列化的属性标记为
[NonSerialized]

另请看“自定义序列化”。除了将类标记为
[Serializable]
,您还需要实现
iSerializable
并添加自定义构造函数,因此:

[Serializable]
public class Foo : ISerializable
{
  // custom deserialization constructor]
  public Foo( SerializationInfo info , StreamingContext context )
  {
    //Get the values from info and assign them to the appropriate properties
  }

  public void GetObjectData( SerializationInfo info , StreamingContext context )
  {
    // populate info with appropriate key/value pairs.
    // Don't forget to explicitly serialize/deserialize and contained complex objects/structures
  }

}

将不应序列化的任何属性标记为
[非序列化]

另请看“自定义序列化”。除了将类标记为
[Serializable]
,您还需要实现
iSerializable
并添加自定义构造函数,因此:

[Serializable]
public class Foo : ISerializable
{
  // custom deserialization constructor]
  public Foo( SerializationInfo info , StreamingContext context )
  {
    //Get the values from info and assign them to the appropriate properties
  }

  public void GetObjectData( SerializationInfo info , StreamingContext context )
  {
    // populate info with appropriate key/value pairs.
    // Don't forget to explicitly serialize/deserialize and contained complex objects/structures
  }

}

您可以实现ISerializable接口,而不仅仅是使用Serializable属性

您可以在此处阅读更多信息:


基本上,您必须实现一个方法—GetObjectData—和一个具有类似签名的构造函数。

您可以实现ISerializable接口,而不仅仅是使用Serializable属性

您可以在此处阅读更多信息:

基本上,您必须实现一个方法——GetObjectData——和一个具有类似签名的构造函数