简体   繁体   English

具有属性的对象反射问题

[英]Object with properties Reflection Issue

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) 我想到的是以下内容:-在每个属性上设置DisplayName属性(到目前为止非常好)-设置自定义属性(示例:useInReport1,useInReport2 ....这将是每个属性上的布尔值)

I would like to know how I am able to achieve the custom properties [useInReport1], [useInReport2] etc.... + retrieve the fields needed only 我想知道我如何能够实现自定义属性[useInReport1],[useInReport2]等... +仅检索需要的字段

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... 改写问题:我如何通过使用自定义属性示例获取属性列表:获取所有标记为Report1 = true的属性,以便读取其值等...

        //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);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM