I got the following situation, I have an object class having multiple properties.
This object is going to be used more than once for reporting purposes, however not all properties are needed, hence I was thinking of using attributes and reflection in order to be able to get the desired properties (for display binding purposes) when needed (instead of hardcoding which fields to use). I would like to use attributes and reflection in order to get the following functionality
What I had in mind is the following: - On each property set the DisplayName attribute (so far so good) - Set a custom property (Example: useInReport1,useInReport2.... which will be a boolean on each property)
I would like to know how I am able to achieve the custom properties [useInReport1], [useInReport2] etc.... + retrieve the fields needed only
Example of my object:
public class ReportObject
{
[DisplayName("Identity")]
[ReportUsage(Report1=true,Report2=true)]
public int ID {get {return _id;}
[DisplayName("Income (Euros)")]
[ReportUsage(Report1=true,Report2=false)]
public decimal Income {get {return _income;}
[DisplayName("Cost (Euros)")]
[ReportUsage(Report1=true,Report2=false)]
public decimal Cost {get {return _cost;}
[DisplayName("Profit (Euros)")]
[ReportUsage(Report1=true,Report2=true)]
public decimal Profit {get {return _profit;}
[DisplayName("Sales")]
[ReportUsage(Report1=false,Report2=true)]
public int NumberOfSales {get {return _salesCount;}
[DisplayName("Unique Clients")]
[ReportUsage(Report1=false,Report2=true)]
public int NumberOfDifferentClients {get {return _clientsCount;}
}
[System.AttributeUsage(AttributeTargets.Property,AllowMultiple=true)]
public class ReportUsage : Attribute
{
private bool _report1;
private bool _report2;
private bool _report3;
public bool Report1
{
get { return _report1; }
set { _report1 = value; }
}
public bool Report2
{
get { return _report2; }
set { _report2 = value; }
}
}
Rephrasing Question: how am I to get a list of properties by using the custom attribute example: get all properties which are taged as Report1 = true in order to read their value etc...