给定其他项目和/或库使用的类:
[Serializable, DataContract]
public class MatchResult
{
[DataMember(Order = 1)]
public long MatchId {get; set;}
[DataMember(Order = 2)]
public long TeamId {get; set;}
[DataMember(Order = 3)]
public MatchStatus Status {get; set;}
[DataMember(Order = 4)]
public List<AgentMatchInfo> AgentMatchInformation {get; set;}
public MatchResult()
{
// I want to make this a private one, without the consumers seeing
// errors that their declaration using
// this current public constructor is no loner applicable.
}
}
当构造函数声明当前由使用者显式调用时,如何正确地使当前的-public
无参数构造函数private
用于protobuf(de(序列化,而不使其成为破坏性的更改?
让当前的public
构造函数有一个可选参数,同时添加一个private
无参数构造函数是一个很好的做法,并且会很好地使用protobuf吗?
Protobuf-net应该可以使用非公共构造函数。你可能在使用一个受限的框架吗?在大多数情况下,如果简单地使用私有构造函数(private MatchResult() {}
(不起作用,我会感到非常惊讶。
另一种选择是完全跳过构造函数,这可以通过使用[ProtoContract(SkipConstructor = true)]
而不是[DataContract]
,使用[ProtoMember(N)]
而不是[DataMember(Order = N)]
来实现——然而,我想知道这是否会在非公共构造函数失败的框架上失败。