Alex Rivera | Logout

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

Asked 2010-03-01T11:02:21.203
12

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)

Edit
Report

1 Answer

2

You can do it like WCF RIA Services. Add an attribute to TestBO, like [Presentation] taking a type as parameter. This new type will redefine the properties, but with the presentation attributes. At run-time, you have to get the identity of the new type and get the custom attributes of its properties.

Or forget about the attribute and have a dictionary mapping the BO with the presentation BO class. This presentation BO class does the same thing as above, i.e. redefine properties with custom attributes.

the presentation BO class is never instantiated, it is simply reflected upon to get presentation info.

answered 2010-03-01T11:10:09.487

Your Answer