获取自定义从LAMBDA属性ex pression属性 [英] Get Custom Attributes from Lambda Property Expression

查看:194
本文介绍了获取自定义从LAMBDA属性ex pression属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC 2 preVIEW 2和编写自定义的HtmlHelper扩展方法来创建使用前pression的标签。 tModel的是从属性和属性的简单类可以有属性定义的验证要求。我试图找出是否对物业存在一定的属性前pression重新presents在我的标签的方法。

在code为类和标签是:

 公共类MyViewModel
{
    [需要]
    公共字符串myProperty的{搞定;组; }
}公共静态MvcHtmlString标签和LT;的TModel,TProperty>(此的HtmlHelper<的TModel>的HtmlHelper,防爆pression< Func键<的TModel,TProperty>>前pression,串标)
{
    返回MvcHtmlString.Create(string.Concat(&下;标签= \\,购pression.GetInputName(),\\>中,标签&所述; /标签>));
}公共静态字符串GetInputName<的TModel,TProperty>(此例pression<&Func键LT;的TModel,TProperty>>前pression)
{
    返回前pression.Body.ToString()子串(如pression.Parameters [0] .Name.Length + 1)。
}

然后我会打电话标签是这样的:

  Html.Label(X => x.MyProperty,我的标签)

有没有办法找出是否在传递给标签法恩pression值的属性具有所需的属性?

我想通了,做下列情况如果存在的话让我的属性,但我希望有做到这一点更清洁的方式。

 公共静态MvcHtmlString标签和LT;的TModel,TProperty>(此的HtmlHelper<的TModel>的HtmlHelper,防爆pression< Func键<的TModel,TProperty>>前pression,串标签)
{
    System.Attribute.GetCustomAttribute(Ex$p$pssion.Property(Ex$p$pssion.Parameter(ex$p$pssion.Parameters[0].Type,前pression.GetInputName()),前pression.GetInputName())。会员的typeof(RequiredAttribute标签))    返回MvcHtmlString.Create(string.Concat(&下;标签= \\,购pression.GetInputName(),\\>中,标签&所述; /标签>));
}


解决方案

您的前pression分析逻辑可以使用一些工作。而不是处理实际的类型,要转换为字符串。

下面是一组的,你可能会改为使用扩展方法。所述第一获取部件的名称。在第二/第三结合以检查该属性是构件上。的getAttribute将返回请求的属性或为空,且IsRequired只检查特定属性。

 公共静态类防爆pressionHelpers
{
公共静态字符串MemberName< T,V>(此例pression<&Func键LT; T,V>>前pression)
{
VAR memberEx pression =前pression.Body作为MemberEx pression;
如果(memberEx pression == NULL)
抛出新的InvalidOperationException异常(前pression必须是当然成员pression);返回memberEx pression.Member.Name;
}公共静态ŧ的getAttribute< T>(这ICustomAttributeProvider提供商)
其中T:属性
{
VAR属性= provider.GetCustomAttributes(typeof运算(T),TRUE);
返回attributes.Length> 0?属性[0]为T:空;
}公共静态布尔IsRequired< T,V>(此例pression<&Func键LT; T,V>>前pression)
{
VAR memberEx pression =前pression.Body作为MemberEx pression;
如果(memberEx pression == NULL)
抛出新的InvalidOperationException异常(前pression必须是当然成员pression);返回memberEx pression.Member.GetAttribute<&RequiredAttribute标签GT;()!= NULL;
}
}

希望这有助于你出去。

I am using ASP.NET MVC 2 Preview 2 and have written a custom HtmlHelper extension method to create a label using an expression. The TModel is from a simple class with properties and the properties may have attributes to define validation requirements. I am trying to find out if a certain attribute exists on the property the expression represents in my label method.

The code for the class and label is:

public class MyViewModel
{
    [Required]
    public string MyProperty { get; set; }
}

public static MvcHtmlString Label<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string label)
{
    return MvcHtmlString.Create(string.Concat("<label for=\"", expression.GetInputName(), "\">", label, "</label>"));
}

public static string GetInputName<TModel, TProperty>(this Expression<Func<TModel, TProperty>> expression)
{
    return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
}

Then I would call the label like this:

Html.Label(x => x.MyProperty, "My Label")

Is there a way to find out if the property in the expression value passed to the Label method has the Required attribute?

I figured out that doing the following does get me the attribute if it exists, but I am hopeful there is a cleaner way to accomplish this.

public static MvcHtmlString Label<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string label)
{
    System.Attribute.GetCustomAttribute(Expression.Property(Expression.Parameter(expression.Parameters[0].Type, expression.GetInputName()), expression.GetInputName()).Member, typeof(RequiredAttribute))

    return MvcHtmlString.Create(string.Concat("<label for=\"", expression.GetInputName(), "\">", label, "</label>"));
}

解决方案

Your expression parsing logic could use some work. Rather than deal with the actual types, you are converting to strings.

Here is a set of extension methods that you might use instead. The first gets the name of the member. The second/third combine to check if the attribute is on the member. GetAttribute will return the requested attribute or null, and the IsRequired just checks for that specific attribute.

public static class ExpressionHelpers
{
	public static string MemberName<T, V>(this Expression<Func<T, V>> expression)
	{
		var memberExpression = expression.Body as MemberExpression;
		if (memberExpression == null)
			throw new InvalidOperationException("Expression must be a member expression");

		return memberExpression.Member.Name;
	}

	public static T GetAttribute<T>(this ICustomAttributeProvider provider) 
		where T : Attribute
	{
		var attributes = provider.GetCustomAttributes(typeof(T), true);
		return attributes.Length > 0 ? attributes[0] as T : null;
	}

	public static bool IsRequired<T, V>(this Expression<Func<T, V>> expression)
	{
		var memberExpression = expression.Body as MemberExpression;
		if (memberExpression == null)
			throw new InvalidOperationException("Expression must be a member expression");

		return memberExpression.Member.GetAttribute<RequiredAttribute>() != null;
	}
}

Hopefully this helps you out.

这篇关于获取自定义从LAMBDA属性ex pression属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆