是否可以从定义的模型访问现有的序列化模型,然后向其附加一个 Contracted 但未知的子类型,然后将其用于序列化?
还是我需要明确创建一个新模型?即在这种情况下为 ModuleTwoFoo
更新
var model = TypeModel.Create();
model[typeof(IFooChild)].AddSubType(2, typeof(ModuleTwoFooChild));
model.CompileInPlace();
RuntimeModel.Default[] // model[] 是访问... Doh!
如果有的话,我唯一的抱怨......希望有一种方法可以访问原始模型然后在其上构建而不是再次重新生成模型
正如马克所说“基本上需要的是克隆但未冻结的模型”
public interface IFoo
{
int FooNumber { get; set; }
IList<IFooChild> Children { get; set; }
}
[ProtoContract, ProtoInclude(1, typeof(ModuleOneFooChild))]
public interface IFooChild
{
IFoo Foo { get; set; }
string Detail { get; set; }
}
[ProtoContract]
public class ModuleOneFoo : IFoo
{
[ProtoMember(1)]
public int FooNumber { get; set; }
[ProtoMember(2)]
public IList<IFooChild> Children { get; set; }
}
[ProtoContract]
public class ModuleOneFooChild : IFooChild
{
public IFoo Foo { get; set; }
[ProtoMember(1)]
public string Detail { get; set; }
}
[ProtoContract]
public class ModuleTwoFoo : IFoo
{
[ProtoMember(1)]
public int FooNumber { get; set; }
[ProtoMember(2)]
public IList<IFooChild> Children { get; set; }
}
[ProtoContract]
public class ModuleTwoFooChild : IFooChild
{
public IFoo Foo { get; set; }
[ProtoMember(1)]
public string Detail { get; set; }
}