Get by reflection properties of class attribute
I find classes inherit from interface :
var baseType = typeof(ICustomSerialization);
Assembly assembly = baseType.Assembly;
var subClass = assembly.GetTypes().Where(t => t.IsSubclassOf(baseType) );
If class have attribute and parameter Name:
[CustomAttribute(Name="Soap")]
class CustomSoapSerializer : ICustomSerialization
It is a way to get by ref开发者_Python百科lection Name property of this attribute?
Try this
public static class CustomAttributeProviderExtensions
{
public static TAttribute[] GetCustomAttributes<TAttribute>(this ICustomAttributeProvider self)
where TAttribute:Attribute
{
return (TAttribute[])self.GetCustomAttributes(typeof(TAttribute), true);
}
}
And the usage
var baseType = typeof(ICustomSerialization);
Assembly assembly = baseType.Assembly;
var subClass = assembly.GetTypes().Where(t => baseType.IsAssignableFrom(t))
.Where(t=>t.GetCustomAttributes<CustomAttribute>().Any(x=>x.Name == "Soap"))
.ToList();
精彩评论