从 C# 中的自定义字段属性获取值

发布于 2024-11-14 22:55:23 字数 2035 浏览 3 评论 0原文

今天早上,我开始了我认为使用自定义字段属性的快速练习。在尝试了很多事情并搜索了很多示例(大多数涉及类而不是字段属性)后,我正式陷入困境。

我的代码如下。一个特点是该类是使用类构建器在 FileHelpers 中构建的。我的各种部分成功的尝试确实设法从此类中获取字段名,所以我相信该部分工作正常。

我想要做的(根据代码中的注释)是 a) 运行字段,b) 对于每个字段,查看 DBDataTypeAttribute 属性是否存在,以及 c) 看似最困难的部分 - 从属性获取值(FieldType 字符串)和AllowNulls 布尔值)。

任何意见表示赞赏!

标记

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫人生路 2024-11-21 22:55:23

很简单;你必须从字段中获取它们,而不是类型。

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

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.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文