c# - Derive/Override the DataMemberAttribute
问题描述
I'd like to override some of the functionality of the DataMemberAttribute but cannot create a derived class because it is marked as 'sealed'.
What I would like to do is have EmitDefaultValue be defaulted to 'false' instead of 'true' and I'd also like to not "emit" a collection if it is null or empty.
Any ideas?
Commenter wanted an example. There really isn't a way to show an example and I believe my question is fairly clear. But what the heck, here goes.
Instead of having to do this on every property:
[DataContract]
public class MyDto
{
[DataMember( EmitDefaultValue = false )]
public string Name { get; set; }
[DataMember( EmitDefaultValue = false )]
public string Occupation { get; set; }
[DataMember( EmitDefaultValue = false )]
public List<string> Friends { get; set; }
}
I'd like to be able to do this:
[DataContract]
public class MyDto
{
[CustomSerialization]
public string Name { get; set; }
[CustomSerialization]
public string Occupation { get; set; }
[CustomSerialization]
public List<string> Friends { get; set; }
}
And for the last property "Friends", if it is null or empty, to not serialize it in the output.
解决方案
推荐阅读
- react-native - 反应原生灯箱中心图像
- php - “遇到 PHP 错误:未定义索引”的问题
- php - 如何从 MySQL 中以一对多关系相关的多个表中查找搜索记录?
- select - 如何在移动设备上拖动以选择多个表格单元格?
- splunk - 如何在查询中引用 eval 变量
- .net - 执行缓存操作失败(所有分区所有者都被留下网格分区数据已丢失)
- angular - Nb-select 多重控制的问题
- parsing - FIRST SET 是否有可能多次包含相同的终端
- c++ - /usr/bin/ld: 找不到 -ldlib /usr/bin/ld: 找不到 -lcblas /usr/bin/ld: 找不到 -llapack
- firebase - 如何使用 Firebase 分析事件参数获取用户电子邮件?