mardi 9 mai 2017

c# find all properties which had seted attribute [duplicate]

This question already has an answer here:

I have attribute class

[AttributeUsage(AttributeTargets.Property)]
public class ExcelSqlColumnsAttribute : Attribute
{
    public readonly string excelColumn;
    public readonly string sqlColumn;

    public ExcelSqlColumnsAttribute(string _excelColumn, string _sqlColumn)
    {
        excelColumn = _excelColumn;
        sqlColumn = _sqlColumn;
    }

    private string topic;
    public string Topic
    {
        get
        {
            return topic;
        }
        set
        {

            topic = value;
        }
    }
}

And i use this attribute class on properties of my model.

public class ExcelSqlColumns
{
    int test;

    [ExcelSqlColumnsAttribute("test","test")]
    public int Test
    {
        get
        {
            return test;
        }
        set
        {
            test = value;
        }
    }

    int test2;

    [ExcelSqlColumnsAttribute("test2", "test2")]
    public int Test2
    {
        get
        {
            return test2;
        }
        set
        {
            test2 = value;
        }
    }

}

How can I use "foreach" for every property where my attribute ExcelSqlColumnsAttribute exists.

So I need to return collection of values, which have this attribute values and value of property (field). Is this possible?





Aucun commentaire:

Enregistrer un commentaire