.Net:如何使用 TypeDescriptor.GetProperties 获取自定义属性?
我创建了自己的属性来装饰我的对象。
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Type.GetProperties(type, Attributes[]) 方法仅返回使用指定属性数组作为过滤器,指定类型组件的属性集合。
您确定目标类型具有用您的自定义属性标记的属性,如下所示?
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?
更新以获取属性属性
此代码是从 复制并粘贴的MSDN,这是 google 搜索“获取自定义属性反射 C#'
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#'