来自 Properties 的 C# 自定义属性

发布于 2024-12-28 07:13:48 字数 1326 浏览 1 评论 0原文

所以我有一个来自我的类​​的属性集合,我想循环遍历它。 对于每个属性,我可能有自定义属性,因此我想循环遍历这些属性。 在这种特殊情况下,我的城市类上有一个自定义属性,

public class City
{   
    [ColumnName("OtroID")]
    public int CityID { get; set; }
    [Required(ErrorMessage = "Please Specify a City Name")]
    public string CityName { get; set; }
}

属性是这样定义的

[AttributeUsage(AttributeTargets.All)]
public class ColumnName : System.Attribute
{
    public readonly string ColumnMapName;
    public ColumnName(string _ColumnName)
    {
        this.ColumnMapName= _ColumnName;
    }
}

当我尝试循环属性[效果很好],然后循环属性时,它只会忽略属性的 for 循环,什么也不返回。

foreach (PropertyInfo Property in PropCollection)
//Loop through the collection of properties
//This is important as this is how we match columns and Properties
{
    System.Attribute[] attrs = 
        System.Attribute.GetCustomAttributes(typeof(T));
    foreach (System.Attribute attr in attrs)
    {
        if (attr is ColumnName)
        {
            ColumnName a = (ColumnName)attr;
            var x = string.Format("{1} Maps to {0}", 
                Property.Name, a.ColumnMapName);
        }
    }
}

当我转到具有自定义属性的属性的直接窗口时,我可以这样做

?Property.GetCustomAttributes(true)[0]

它将返回 ColumnMapName: "OtroID"

虽然我似乎无法使其以编程方式工作

so I have a collection of Properties from my class which I want to loop through.
For each property I may have custom attributes so I want to loop through those.
In this particular case I have a custom attribute on my City Class as such

public class City
{   
    [ColumnName("OtroID")]
    public int CityID { get; set; }
    [Required(ErrorMessage = "Please Specify a City Name")]
    public string CityName { get; set; }
}

The attribute is defined as such

[AttributeUsage(AttributeTargets.All)]
public class ColumnName : System.Attribute
{
    public readonly string ColumnMapName;
    public ColumnName(string _ColumnName)
    {
        this.ColumnMapName= _ColumnName;
    }
}

When I try to loop through the properties [which works fine] and then loop through the attributes it just ignores the for loop for the attribute and returns nothing.

foreach (PropertyInfo Property in PropCollection)
//Loop through the collection of properties
//This is important as this is how we match columns and Properties
{
    System.Attribute[] attrs = 
        System.Attribute.GetCustomAttributes(typeof(T));
    foreach (System.Attribute attr in attrs)
    {
        if (attr is ColumnName)
        {
            ColumnName a = (ColumnName)attr;
            var x = string.Format("{1} Maps to {0}", 
                Property.Name, a.ColumnMapName);
        }
    }
}

When I go to the immediate window for the property that has a custom attribute I can do

?Property.GetCustomAttributes(true)[0]

It will return ColumnMapName: "OtroID"

I can't seem to fit this to work programmatically though

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

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

发布评论

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

评论(4

追星践月 2025-01-04 07:13:48

我相信你想这样做:

PropertyInfo[] propCollection = type.GetProperties();
foreach (PropertyInfo property in propCollection)
{
    foreach (var attribute in property.GetCustomAttributes(true))
    {
        if (attribute is ColumnName)
        {
        }
    }
}

You want to do this I believe:

PropertyInfo[] propCollection = type.GetProperties();
foreach (PropertyInfo property in propCollection)
{
    foreach (var attribute in property.GetCustomAttributes(true))
    {
        if (attribute is ColumnName)
        {
        }
    }
}
倾听心声的旋律 2025-01-04 07:13:48

应作者要求,从原始问题的评论中重新发布

只是出于兴趣,typeof(T) 中的 T 是什么?

在立即窗口中,您正在调用 Property.GetCustomAttribute(true)[0],但在 foreach 循环内,您正在对类型参数调用 GetCustomattributes。

这行:

System.Attribute[] attrs = System.Attribute.GetCustomAttributes(typeof(T));

应该是这样

System.Attribute[] attrs = property.GetCustomAttributes(true);

最好的问候,

Repost from Comments of original question, at authors request

Just out of interest what is T in typeof(T)?

In the immediate window you are calling Property.GetCustomAttribute(true)[0] but inside the foreach loop you are calling GetCustomattributes on a typeparameter instead.

This line:

System.Attribute[] attrs = System.Attribute.GetCustomAttributes(typeof(T));

Ought to be this

System.Attribute[] attrs = property.GetCustomAttributes(true);

Best regards,

最初的梦 2025-01-04 07:13:48

我得到的这段代码最终的结果是 x 的值为 “OtroID Maps to CityID”

var props = typeof(City).GetProperties();
foreach (var prop in props)
{
    var attributes = Attribute.GetCustomAttributes(prop);
    foreach (var attribute in attributes)
    {
        if (attribute is ColumnName)
        {
            ColumnName a = (ColumnName)attribute;
            var x = string.Format("{1} Maps to {0}",prop.Name,a.ColumnMapName);
        }
    }
}

I got this code to end up with the value of x being "OtroID Maps to CityID".

var props = typeof(City).GetProperties();
foreach (var prop in props)
{
    var attributes = Attribute.GetCustomAttributes(prop);
    foreach (var attribute in attributes)
    {
        if (attribute is ColumnName)
        {
            ColumnName a = (ColumnName)attribute;
            var x = string.Format("{1} Maps to {0}",prop.Name,a.ColumnMapName);
        }
    }
}
—━☆沉默づ 2025-01-04 07:13:48

从内部来看,您应该研究属性,而不是 typeof(T)。

使用智能感知并查看可以从 Property 对象调用的方法。

Property.GetCustomAttributes(Boolean) 可能对您很重要。
这将返回一个数组,您可以在其上使用 LINQ 来快速返回符合您要求的所有属性。

In the inner look, you should be investigating the Properties, not the typeof(T).

Use the intellisense and take a look at the methods you can call off of the Property objects.

Property.GetCustomAttributes(Boolean) might be of importance to you.
This will return an array, and you can use LINQ on it to quickly return all the attributes that match your requirements.

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