• <i id='y6vU3'><tr id='y6vU3'><dt id='y6vU3'><q id='y6vU3'><span id='y6vU3'><b id='y6vU3'><form id='y6vU3'><ins id='y6vU3'></ins><ul id='y6vU3'></ul><sub id='y6vU3'></sub></form><legend id='y6vU3'></legend><bdo id='y6vU3'><pre id='y6vU3'><center id='y6vU3'></center></pre></bdo></b><th id='y6vU3'></th></span></q></dt></tr></i><div id='y6vU3'><tfoot id='y6vU3'></tfoot><dl id='y6vU3'><fieldset id='y6vU3'></fieldset></dl></div>
      <bdo id='y6vU3'></bdo><ul id='y6vU3'></ul>
    1. <legend id='y6vU3'><style id='y6vU3'><dir id='y6vU3'><q id='y6vU3'></q></dir></style></legend>

      <small id='y6vU3'></small><noframes id='y6vU3'>

        <tfoot id='y6vU3'></tfoot>

      1. 从 Lambda 属性表达式获取自定义属性

        Get Custom Attributes from Lambda Property Expression(从 Lambda 属性表达式获取自定义属性)

          <small id='drRlE'></small><noframes id='drRlE'>

            <tbody id='drRlE'></tbody>

                <bdo id='drRlE'></bdo><ul id='drRlE'></ul>
              • <tfoot id='drRlE'></tfoot>
                <legend id='drRlE'><style id='drRlE'><dir id='drRlE'><q id='drRlE'></q></dir></style></legend>

                • <i id='drRlE'><tr id='drRlE'><dt id='drRlE'><q id='drRlE'><span id='drRlE'><b id='drRlE'><form id='drRlE'><ins id='drRlE'></ins><ul id='drRlE'></ul><sub id='drRlE'></sub></form><legend id='drRlE'></legend><bdo id='drRlE'><pre id='drRlE'><center id='drRlE'></center></pre></bdo></b><th id='drRlE'></th></span></q></dt></tr></i><div id='drRlE'><tfoot id='drRlE'></tfoot><dl id='drRlE'><fieldset id='drRlE'></fieldset></dl></div>
                  本文介绍了从 Lambda 属性表达式获取自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 ASP.NET MVC 2 Preview 2 并编写了自定义 HtmlHelper 扩展方法来使用表达式创建标签.TModel 来自一个具有属性的简单类,并且这些属性可能具有定义验证要求的属性.我试图找出表达式在我的标签方法中表示的属性上是否存在某个属性.

                  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.

                  类和标签的代码是:

                  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")
                  

                  有没有办法判断传递给Label方法的表达式值中的属性是否有Required属性?

                  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.

                  这里有一组您可以使用的扩展方法.第一个获取成员的名称.第二个/第三个组合检查属性是否在成员上.GetAttribute 将返回请求的属性或 null,而 IsRequired 仅检查该特定属性.

                  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;
                      }
                  }
                  

                  希望这对您有所帮助.

                  这篇关于从 Lambda 属性表达式获取自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                  Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)
                    <bdo id='iFnoZ'></bdo><ul id='iFnoZ'></ul>
                  • <i id='iFnoZ'><tr id='iFnoZ'><dt id='iFnoZ'><q id='iFnoZ'><span id='iFnoZ'><b id='iFnoZ'><form id='iFnoZ'><ins id='iFnoZ'></ins><ul id='iFnoZ'></ul><sub id='iFnoZ'></sub></form><legend id='iFnoZ'></legend><bdo id='iFnoZ'><pre id='iFnoZ'><center id='iFnoZ'></center></pre></bdo></b><th id='iFnoZ'></th></span></q></dt></tr></i><div id='iFnoZ'><tfoot id='iFnoZ'></tfoot><dl id='iFnoZ'><fieldset id='iFnoZ'></fieldset></dl></div>

                    <tfoot id='iFnoZ'></tfoot><legend id='iFnoZ'><style id='iFnoZ'><dir id='iFnoZ'><q id='iFnoZ'></q></dir></style></legend>
                      <tbody id='iFnoZ'></tbody>

                        <small id='iFnoZ'></small><noframes id='iFnoZ'>