Vin*_*nzz 7 c# inheritance custom-attributes
我在通用DataGrids中显示Business Object,我想通过自定义属性设置列标题,如:
class TestBo
{
[Header("NoDisp")]
public int ID {get; set;}
[Header("Object's name")]
public String Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这么好,但我还想通过继承将我的显示与我的数据分开:
class TestBO
{
public int ID {get; set;}
public String Name { get; set; }
}
class TestPresentationBO : TestBO
{
//Question: how to simply set the Header attribute on the different properties?
}
Run Code Online (Sandbox Code Playgroud)
我通过Child构造函数中的SetCustomAttribute通过反射看到了一个解决方案,但它会很麻烦,所以这个问题有一个简单而优雅的技巧吗?
请阻止我打破数据/演示文稿的分离; o)
问题:如何在不同的属性上简单设置Header属性?
无法按照建议的方式在继承的成员上设置属性,因为属性特定于类型.SetCustomAttribute对你没有帮助 - 在运行时构造新类型时它只是有用的.一旦编译了属性,就无法在运行时更改它,因为它是元数据的一部分.
如果你想保持分离,你将不得不寻找另一种方式.
(您可以将属性设置为虚拟,在Presentation类中覆盖它们并在覆盖上添加属性,但这看起来很狡猾并且实际上没有任何分离 - 无论如何,您最终都会在TestPresentationBO中使用完整的TestBO类...)
| 归档时间: |
|
| 查看次数: |
9970 次 |
| 最近记录: |