我有几个包含各种属性的类。这里有一个例子:如何获取嵌入在自定义属性中的类型?
[XmlInclude(typeof(AFReader))]
[XmlInclude(typeof(SQLReader))]
[XmlInclude(typeof(MySQLReader))]
[Serializable]
[DataContract]
public class DataSource
{
...
}
我需要能够通过这些属性来过滤和选择其BaseType
的是,在这里它继承(DataSource
在这种情况下)的类型。
所以最后,我想是这样的:
static private List<Type> AttributeFilter(IEnumerable<Attribute> attributes, Type baseType)
{
List<Type> filteredAttributes = new List<Type>();
foreach (Attribute at in attributes)
{
// if (at.TypeId.GetType().BaseType == baseType)
// filteredAttributes.Add(at.GetType());
// if (at.GetType().BaseType == baseType)
// filteredAttributes.Add(at.GetType());
}
return filteredAttributes;
}
与调用:
Type test = typeof(DataSource);
IEnumerable<Attribute> customAttributes = test.GetCustomAttributes();
List<Type> filteredAttributes = AttributeFilter(customAttributes, test);