TypeScript类实体的命名约定表示,我应该在TypeScript中使用camelCase作为属性名称。
就像:
export class Bird {
type: string;
nameOfBird: string;
}
但是C#中的命名约定说,我应该在类属性前面加一个_:
public class Bird {
public string _type {get; set;}
public string _nameOfBird {get; set;}
}
但是,当在我的应用程序之间以JSON形式发送时,我会遇到冲突,因为我不知道我应该在JSON对象中使用camelCase还是_case。这似乎也让编组变得更加困难。
你是怎么处理的?忽略其中一个指导方针,还是在两者之间进行编组?
只需使用序列化属性即可实现这两个功能:
public class Bird {
[JsonProperty("type")]
public string _type {get; set;}
[JsonProperty("nameOfBird")]
public string _nameOfBird {get; set;}
}
甚至
[JsonProperty("type")]
public string AnyCompletelyDifferentName { get; set; }
除此之外,C#中没有关于公共成员前缀的约定。然而,也许你的公司里也有这样一个。
我不知道属性的_前缀是C#命名约定,但是,假设你是对的,至少对你的公司来说,有一种非常好的方法可以解决你刚才提出的矛盾。创建:
private string _nameOfBird;
public string nameOfBird {
get {
return _nameOfBird;
}
set {
_nameOfBird = value;
}
}
这样你就尊重了这两种惯例。