如何解决怎样才能向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);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。