想象一下以下代码示例:
[ProtoContract]
[ProtoInclude(1, typeof(ExampleState))]
public abstract class BaseState<T>
{
[ProtoMember(2)]
public T Model { get; set; }
[ProtoMember(3)]
public string Id => this.Model.Id;
}
[ProtoContract]
public class ExampleState: BaseState<ExampleModel>
{
[ProtoMember(1)]
public DateTimeOffset LastSeenAt { get; set; }
}
[ProtoContract]
public class ExampleModel
{
[ProtoMember(1)]
public DateTimeOffset CreatedAt { get; set; }
[ProtoMember(2)]
public string Name { get; set; }
}
使用 protobuf-net 时。
BaseState<T>
为 ProtoInclude
定义了一些 ExampleState
,但随后会显示警告 PBN0012: The type 'ExampleState' is declared as an include, but is not a direct sub-type
。
这是期望的行为还是错误?无需重新编写所有模型的解决方法是什么样的(在我们的例子中,这可能会变得非常复杂)。
我们遇到了类似的问题,并在 GitHub
上找到了解决方案/示例简而言之,您需要一个使用以下示例语法的抽象基类:
[ProtoContract]
[ProtoInclude(1, typeof(ResBase<int>), DataFormat = DataFormat.Group)]
[ProtoInclude(2, typeof(ResBase<string>), DataFormat = DataFormat.Group)]
public abstract class ResBase {}