.Net:如何使用 TypeDescriptor.GetProperties 获取自定义属性?

发布于 2024-12-13 06:53:48 字数 450 浏览 0 评论 0原文

我创建了自己的属性来装饰我的对象。

 [AttributeUsage(AttributeTargets.All)]
    public class MyCustomAttribute : System.Attribute { }

当我尝试使用 TypeDescriptor.GetProperties 传入自定义属性时,即使类型用该属性修饰,它也不会返回任何内容。

  var props = TypeDescriptor.GetProperties(
              type, 
              new[] { new Attributes.FlatLoopValueInjection()});

如何让 TypeDescriptor.GetProperties 识别我的自定义类型?

I have created my own attribute to decorate my object.

 [AttributeUsage(AttributeTargets.All)]
    public class MyCustomAttribute : System.Attribute { }

When I try to use TypeDescriptor.GetProperties passing in my custom attribute it doesn't return anything even though the type is decorated with the attribute.

  var props = TypeDescriptor.GetProperties(
              type, 
              new[] { new Attributes.FlatLoopValueInjection()});

How do I get TypeDescriptor.GetProperties to recognize my custom types?

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

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

发布评论

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

评论(2

岁吢 2024-12-20 06:53:49

Type.GetProperties(type, Attributes[]) 方法仅返回使用指定属性数组作为过滤器,指定类型组件的属性集合。
您确定目标类型具有用您的自定义属性标记的属性,如下所示?

//...
    var props = TypeDescriptor.GetProperties(typeof(Person), new Attribute[] { new NoteAttribute() });
    PropertyDescriptor nameProperty = props["Name"];
}
//...
class Person {
    [Note]
    public string Name { get; set; }
}
//...
class NoteAttribute : Attribute {
/* implementation */
}

The Type.GetProperties(type, Attributes[]) method returns only the collection of properties for a specified type of component using a specified array of attributes as a filter.
Are you sure target type has a properties marked with your custom attributes, like this?

//...
    var props = TypeDescriptor.GetProperties(typeof(Person), new Attribute[] { new NoteAttribute() });
    PropertyDescriptor nameProperty = props["Name"];
}
//...
class Person {
    [Note]
    public string Name { get; set; }
}
//...
class NoteAttribute : Attribute {
/* implementation */
}
ι不睡觉的鱼゛ 2024-12-20 06:53:49

更新以获取属性属性

此代码是从 复制并粘贴的MSDN,这是 google 搜索“获取自定义属性反射 C#'

using System;

public class ExampleAttribute : Attribute
{
    private string stringVal;

    public ExampleAttribute()
    {
        stringVal = "This is the default string.";
    }

    public string StringValue
    {
        get { return stringVal; }
        set { stringVal = value; }
    }
}

[Example(StringValue="This is a string.")]
class Class1
{
    public static void Main()
    {
        PropertyInfo propertyInfo = typeof (Class1).GetProperties().Where(p => p.Name == "Foo").FirstOrDefault();
        foreach (object attrib in propertyInfo.GetCustomAttributes(true))
        {
            Console.WriteLine(attrib);
        }
    }

    [Example(StringValue = "property attribute")]
    public string Foo {get;set;}
}

updated to get property attribute

this code was copy and pasted from MSDN, which was the first result of the google search 'get customattribute reflection c#'

using System;

public class ExampleAttribute : Attribute
{
    private string stringVal;

    public ExampleAttribute()
    {
        stringVal = "This is the default string.";
    }

    public string StringValue
    {
        get { return stringVal; }
        set { stringVal = value; }
    }
}

[Example(StringValue="This is a string.")]
class Class1
{
    public static void Main()
    {
        PropertyInfo propertyInfo = typeof (Class1).GetProperties().Where(p => p.Name == "Foo").FirstOrDefault();
        foreach (object attrib in propertyInfo.GetCustomAttributes(true))
        {
            Console.WriteLine(attrib);
        }
    }

    [Example(StringValue = "property attribute")]
    public string Foo {get;set;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文