C# preprocessor directives:
をご利用くださいあなたは、アプリケーション全体の周りにそれをしなければならない場合
public class Dto
{
#if !DEBUG
[JsonProperty("l")]
#endif
public string LooooooooooooongName { get; set; }
}
ОкEDITは、多分これは非常に便利ではありません。もう1つのより便利な方法は、カスタムContractResolver
を実装し、このプリプロセッサディレクティブを1か所に配置することです。
public class CustomContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var prop = base.CreateProperty(member, memberSerialization);
#if DEBUG
if(prop != null)
{
// If in debug mode -> return PropertyName value to the initial member name.
prop.PropertyName = member.Name;
}
#endif
return prop;
}
}
と使用方法:
var jsonString = JsonConvert.SerializeObject(someObj, new JsonSerializerSettings
{
ContractResolver = new CustomContractResolver(),
});
注:あなたはJsonConverter周りのラッパーを実装するか、デフォルトのJSONシリアライザの設定を使用するので、あなたは文句を言わない契約リゾルバを毎回指定する必要がありますすることができます。
あなたは、すべてのクラスで一度にあなたのプログラム全体を意味しますか? – tia
はい、私は数十のDTOを持っています –