从自定义字段值获取在C#中的属性 [英] Getting values from Custom Field Attributes in C#

查看:152
本文介绍了从自定义字段值获取在C#中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天上午,我什么我以为会是一个快速的锻炼,使用自定义字段属性进发。尝试过很多事情和搜索的例子很多(大部分涉及类,而不是字段属性),我正式卡住了。

我的code如下。一个特点是,该类建在使用classbuilder FileHelpers。我的各种部分成功的尝试还是设法从这个类中的字段名的,所以我相信这部分工作正常。

我想要做的(每在code注释)什么是)在田野运行,b)每一,看是否DBD​​ataTypeAttribute属性存在,以及c)看似最难的部分 - 得到的值从属性(的FieldType字符串,AllowNulls布尔)。

任何意见AP preciated!

标记

 类节目
{
    静态无效的主要(字串[] args)
    {
        //所需的输出:
        System.Type的用户等级和积分= NULL;
        用户等级和积分= ClassBuilder.ClassFromString(@
                                                公共类ExpenseReport
                                                {
                                                    [FieldQuoted('',QuoteMode.OptionalForRead,MultilineMode.AllowForRead)]
                                                    [DBDataTypeAttribute(的FieldType =VARCHAR(1000),AllowNulls = TRUE)]
                                                    公共字符串的UniqueID;
                                                    [FieldQuoted('',QuoteMode.OptionalForRead,MultilineMode.AllowForRead)]
                                                    公共字符串ERNu​​m;
                                                });        [对象]属性;
        属性= userType.GetCustomAttributes(typeof运算(DBDataTypeAttribute),TRUE);
        的foreach(在属性对象属性)
        {
            //想能够ID为每个字段DBDataTypeAttribute是present,并获得的FieldType和AllowNulls值            DBDataTypeAttribute A =(DBDataTypeAttribute)属性;
            Console.WriteLine(属性,a.FieldType);
            到Console.ReadLine();        }
    }
}[AttributeUsage(AttributeTargets.Field)
公共类DBDataTypeAttribute:System.Attribute
{
    私人字符串的字段类型;
    公共字符串的FieldType
    {
        {返回的字段类型; }
    }    私人字符串allownulls;
    公共字符串AllowNulls
    {
        {返回allownulls; }
    }}


解决方案

pretty简单;你必须从领域得到他们,而不是类型。

 的foreach(在userType.GetFields字段信息字段())
{
    DBDataTypeAttribute属性=(DBDataTypeAttribute)Attribute.GetCustomAttribute(场的typeof(DBDataTypeAttribute));
    如果(属性!= NULL)
    {
        //用它做什么。
    }
}

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

}

解决方案

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

这篇关于从自定义字段值获取在C#中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆