Getting values from Custom Field Attributes in C#

廉价感情. 提交于 2019-12-12 08:58:38

问题


This morning I embarked on what I thought would be a quick exercise to use custom field attributes. Having tried many things and searching many examples (most involving class rather than field attributes), I'm officially stuck.

My code is below. One peculiarity is that the class is built in FileHelpers using the classbuilder. My various partially successful attempts did manage to get the fieldnames from this class though, so I believe that part works fine.

What I want to do (per the comment in the code) is a) Run through the fields, b) for each, see if the DBDataTypeAttribute Attribute exists, and c) The seemingly hardest part - getting the values from the attribute (FieldType string, and AllowNulls bool).

Any comments appreciated!

Mark

class Program
{
    static void Main(string[] args)
    {
        // Desired output:
        System.Type userType = null;
        userType = ClassBuilder.ClassFromString(@"
                                                public class ExpenseReport
                                                {
                                                    [FieldQuoted('""', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
                                                    [DBDataTypeAttribute(FieldType = ""varchar(1000)"", AllowNulls = true)]
                                                    public String UniqueID;
                                                    [FieldQuoted('""', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
                                                    public String ERNum;
                                                }");

        object[] attributes;
        attributes = userType.GetCustomAttributes(typeof(DBDataTypeAttribute), true);
        foreach (Object attribute in attributes)
        {
            // Would like to be able to ID for each field whether the DBDataTypeAttribute is present, and get the FieldType and AllowNulls Values

            DBDataTypeAttribute a = (DBDataTypeAttribute)attribute;
            Console.WriteLine("Attribute: ", a.FieldType);
            Console.ReadLine();

        }
    }
}

[AttributeUsage(AttributeTargets.Field)]
public class DBDataTypeAttribute : System.Attribute
{
    private string fieldtype;
    public string FieldType
    {
        get { return fieldtype; }
    }

    private string allownulls;
    public string AllowNulls
    {
        get { return allownulls; }
    }

}

回答1:


Pretty simple; you have to get them from the fields, not the type.

foreach( FieldInfo field in userType.GetFields() )
{
    DBDataTypeAttribute attribute = (DBDataTypeAttribute)Attribute.GetCustomAttribute(field, typeof(DBDataTypeAttribute));
    if( attribute != null )
    {
        // Do something with it.
    }
}


来源:https://stackoverflow.com/questions/6320591/getting-values-from-custom-field-attributes-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!