【发布时间】:2016-04-20 18:27:55
【问题描述】:
例如我有一个简单的类
class SomeCfg
{
[XmlAttribute("ArchivePath")]
public string ArchivePath { get; set; }
}
可以看出,给定的类有一个带有 XmlAttribute 的属性。但是当程序执行到那个代码时
var t = typeof (SomeCfg);
var props = t.GetProperties().Where(
prop => Attribute.IsDefined(prop, typeof(XmlAttribute)));
运行时抛出异常
System.ArgumentException: Type passed in must be derived from System.Attribute or System.Attribute itself.
当然,当我看到类型层次结构时,我意识到这是真的(我不会解释为什么它不继承自该类)。
我的问题是如何列出这些属性(在这种情况下,不能创建一个继承自 XmlAttribute 和 System.Attribute 的类)。
根据问题标签.Net版本是4.0。
【问题讨论】:
-
对不起,这条线当然不见了。我更正了这个问题。