怎样才能向protobuf-net指示接口属性应反序列化为一种特定的具体类型?

问题描述

| 我有这个财产:
public class SomeClass
{
  public ISomeInterface SomeProperty { get;set; }
}
现在,在此特定上下文中,
ISomeInterface
只能是特定的具体类型,例如
SomeClass2
。如果没有ProtoInclude属性,我可以这样做吗? 我以为我可以做这样的事情:
model.Add(typeof(SomeClass),true).Add(1,\"SomeProperty\",typeof(SomeClass2));
指出,
SomeProperty
应始终反序列化为
SomeClass2
(当然,它实现了接口)。 但是我在模型上找不到这种方法。     

解决方法

您正在测试我; p 那不是直接暴露出来的。 Google目前不允许我进行提交,但是以下内容将在我可以提交后立即生效:
[Test]
public void ExposeInterfaceWithDefaultImplementation()
{
    var model = TypeModel.Create();
    // note the following sets for the ConstructType for the ISomeInferface,not specifically for Foo
    model.Add(typeof(ISomeInterface),false).Add(\"Foo\").ConstructType = typeof(SomeClass2);
    model.Add(typeof(SomeClass),false).Add(\"SomeProperty\");

    var orig = new SomeClass();
    orig.SomeProperty = new SomeClass2();
    orig.SomeProperty.Foo = 123;
    var clone = (SomeClass)model.DeepClone(orig);
    Assert.AreEqual(123,clone.SomeProperty.Foo);
}