是否可以将扩展方法添加到类属性中以获取与该属性关联的属性的值?
我有几个类,并分配了属性。我最感兴趣的是 FieldLength.MaxLength 值。
/// <summary>
/// Users
/// </summary>
[Table(Schema = "dbo", Name = "users"), Serializable]
public partial class Users
{
/// <summary>
/// Last name
/// </summary>
[Column(Name = "last_name", SqlDbType = SqlDbType.VarChar)]
private string _LastName;
[FieldLength(MaxLength=25), FieldNullable(IsNullable=false)]
public string LastName
{
set { _LastName = value; }
get { return _LastName; }
}
}
我需要知道是否可以为我的类中的属性编写某种扩展方法以返回 FieldLength 属性的 MaxLength 值?
例如。我希望能够写出如下内容......
Users user = new Users();
int lastNameMaxLength = user.LastName.MaxLength();
I have several classes with attributes assigned to them. The one I'm mostly interested in is the FieldLength.MaxLength value.
/// <summary>
/// Users
/// </summary>
[Table(Schema = "dbo", Name = "users"), Serializable]
public partial class Users
{
/// <summary>
/// Last name
/// </summary>
[Column(Name = "last_name", SqlDbType = SqlDbType.VarChar)]
private string _LastName;
[FieldLength(MaxLength=25), FieldNullable(IsNullable=false)]
public string LastName
{
set { _LastName = value; }
get { return _LastName; }
}
}
I need to know if it's possible to write some kind of extension method for the properties in my class to return the MaxLength value of the FieldLength attribute?
For instance. I'd like to be able to write something like the following…
Users user = new Users();
int lastNameMaxLength = user.LastName.MaxLength();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(5)
不会。因为您建议的语法返回 LastName
属性的值,而不是属性本身。
为了检索和使用属性,您需要使用反射,这意味着您需要了解属性本身。
作为一个想法,您可以通过使用 LINQ 的表达式库来解析对象的属性来巧妙地实现此目的。
您可能会寻找示例语法:
var lastNameMaxLength = AttributeResolver.MaxLength<Users>(u => u.LastName);
其中:
public class AttributeResolver
{
public int MaxLength<T>(Expression<Func<T, object>> propertyExpression)
{
// Do the good stuff to get the PropertyInfo from the Expression...
// Then get the attribute from the PropertyInfo
// Then read the value from the attribute
}
}
我发现此类有助于解析表达式的属性:
public class TypeHelper
{
private static PropertyInfo GetPropertyInternal(LambdaExpression p)
{
MemberExpression memberExpression;
if (p.Body is UnaryExpression)
{
UnaryExpression ue = (UnaryExpression)p.Body;
memberExpression = (MemberExpression)ue.Operand;
}
else
{
memberExpression = (MemberExpression)p.Body;
}
return (PropertyInfo)(memberExpression).Member;
}
public static PropertyInfo GetProperty<TObject>(Expression<Func<TObject, object>> p)
{
return GetPropertyInternal(p);
}
}
您可以编写一个扩展方法,但它必须接受 PropertyInfo
的第一个参数,而不是 string
(因为 string
本身没有属性.)它看起来像这样:
public static int GetMaxLength(this PropertyInfo prop)
{
// TODO: null check on prop
var attributes = prop.GetCustomeAttributes(typeof(FieldLengthAttribute), false);
if (attributes != null && attributes.Length > 0)
{
MaxLengthAttribute mla = (MaxLengthAttribute)attributes[0];
return mla.MaxLength;
}
// Either throw or return an indicator that something is wrong
}
然后您可以通过反射获取属性:
int maxLength = typeof(Users).GetProperty("LastName").GetMaxLength();
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
不,这是不可能的。不过,您可以在
Users
上添加扩展方法:No, this is not possible. You could add an extension method on
Users
though: