0

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...

IanCian
  • 1,098
  • 2
  • 16
  • 34
  • Well, how far have you got? Have you declared the attribute classes yet? (These are attributes, not properties - it's really helpful to keep the terminology straight.) Have you tried applying those attributes to your properties? Have you tried checking which properties have attributes applied? We're not going to just write the whole solution for you - please show where you're stuck. – Jon Skeet Sep 07 '15 at 09:39
  • A better question to ask him is if he even bothered googled any thing remotely in that area :) http://stackoverflow.com/questions/2281972/how-to-get-a-list-of-properties-with-a-given-attribute – eran otzap Sep 07 '15 at 10:11
  • thanks eran otzap. was looking at it and it would solve my problem :) edited post so as to have the attribute class also. will let you know whether this would solve the issue since it is the first time i am dealing with reflection. – IanCian Sep 07 '15 at 10:27

1 Answers1

0
        //Get all propertyes of class
        var allProperties = typeof(ReportObject).GetProperties();

        //List that will contain all properties used in specific report
        List<PropertyInfo> filteredProperties = new List<PropertyInfo>();

        foreach(PropertyInfo propertyInfo in allProperties) {
            ReportUsage attribute = propertyInfo.GetCustomAttribute(typeof(ReportUsage), true) as ReportUsage;
            if(attribute != null && attribute.ReportUsage1) { //if you need properties for report1
                filteredProperties.Add(propertyInfo);
            }
        }