Vinzz Vinzz - 15 days ago 5
C# Question

C# add custom attributes for a parent's property in an inherited class

I'm displaying Business Object in generic DataGrids, and I want to set the column header through a custom attribute, like:

class TestBo
{
[Header("NoDisp")]
public int ID {get; set;}

[Header("Object's name")]
public String Name { get; set; }
}


So far, so good, but I'd also want to separate my display from my data, by inheritance:

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?
}


I see a solution via reflection with a SetCustomAttribute in the Child constructor, but it will be cumbersome, so is there a simple and elegant trick for this problem?

Please prevent me from breaking the data/presentation separation ;o)

Answer

Just thinking, can't you solve this with partial classes and the MetadatatypeAttribute? MVC2 uses this pattern for Model validation.