如何序列化接口(protobuf-net)?

时间:2016-04-12 17:00:13

标签: c# serialization protobuf-net

想序列化SomeInterface,它是在另一个dll中声明的SomeClass。还有这个接口的实现。

public interface SomeInterface
{
     SomeClass Instance { get; }
}

public class BasicClass : SomeInterface
{
    public BasicClass(SomeClass instance)
    {
         Instance = instance;
    }

    public SomeClass Instance { get; }

    public override string ToString()
    {
         return Instance.ToDisplayString();
    }

}

如果您使用 Surrogate ,则不能将隐式运算符用于接口

[ProtoContract]
class SomeInterfaceSurrogate
{ 
      [ProtoMember(1)] public int Id { get; set; }

      public static implicit operator SomeInterface(SomeInterfaceSurrogate value)
      {
            return value == null ? null : GetInstanceForId(int id);
      }

      public static implicit operator SomeInterfaceSurrogate(SomeInterface value)
      {   
            return value == null ? null : new SomeInterfaceSurrogate()
            {
                  Id = value.GetId()
            };
      }
}

这些任务的工具是什么?

0 个答案:

没有答案