开发者

How can one indicate to protobuf-net that an interface property should be deserialized as a particular concrete type?

I have this property:

public class SomeClass
{
  public ISomeInterface SomeProperty { get;set; }
}

Now, ISomeInterface in this particular context can only be a specific concrete type, like SomeClass2. Can I do it without the ProtoInclude attribute?

I thought I can do something like this:

model.Add(typeof(SomeClass), tr开发者_开发知识库ue).Add(1, "SomeProperty", typeof(SomeClass2));

Indicating, that SomeProperty should always be deserialized as SomeClass2 (which, of course, implements the interface).

But I failed to find a method like this on the model.


You are out to test me ;p

That wasn't directly exposed; google isn't letting me commit at the moment, but the following will work as soon as I can commit:

[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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜