我已经构建了一个方法,如果在属性上找到它,它会返回一个属性实例:
public static U GetPropertyAttribute<T, TProperty, U>(this T instance, Expression<Func<T, TProperty>> propertySelector, U attribute) where U : Attribute
{
return Attribute.GetCustomAttribute(instance.GetType().GetProperty((propertySelector.Body as MemberExpression).Member.Name), typeof(U), true) as U;
}
为了获得实例,我必须调用:
var cc = new CustomClass();
cc.GetPropertyAttribute(x => x.Name, new NullableAttribute())
它工作正常,我得到了属性类的确切实例。
但是,我不喜欢我必须使用 new NullableAttribute() 作为参数,我希望调用看起来像:
cc.GetPropertyAttribute<NullableAttribute>(x => x.Name)
然而,这不起作用,因为一旦我删除第二个参数并在方法名称中添加 1 个泛型类型,它也开始需要其他两个。有没有办法强制该方法从 3 个通用参数中推断出 2 个?即我想指定属性类,但不想指定类名和属性名。
更新:
这是实现的代码,感谢 Jon,以及字符串解决方案的代码。我决定嵌套这个类,这样如果我为其他一些扩展类引入相同的方法,我就不会污染命名空间。
public static class AttributeExtensions
{
public static ObjectProperty<T, TProperty> From<T, TProperty>(this T instance, Expression<Func<T, TProperty>> propertySelector)
{
return new ObjectProperty<T, TProperty>(instance, propertySelector);
}
public class ObjectProperty<T, TProperty>
{
private readonly T instance;
private readonly Expression<Func<T, TProperty>> propertySelector;
public ObjectProperty(T instance, Expression<Func<T, TProperty>> propertySelector)
{
this.instance = instance;
this.propertySelector = propertySelector;
}
public U GetAttribute<U>() where U : Attribute
{
return Attribute.GetCustomAttribute(instance.GetType().GetProperty((propertySelector.Body as MemberExpression).Member.Name), typeof(U), true) as U;
}
}
public static T GetPropertyAttribute<T>(this object instance, string propertyName) where T : Attribute
{
return Attribute.GetCustomAttribute(instance.GetType().GetProperty(propertyName), typeof(T), true) as T;
}
}
所以现在调用是这样的:
var cc = new CustomClass();
var attr = cc.From(x => x.Name).GetAttribute<NullableAttribute>();