我有一個模型(簡化了相關性)其他模特屬性:無法獲取自定義屬性這樣不是字符串
public abstract class TitleCreateModel : ICreateModel
{
[Required]
[MaxLength(400)]
public string TitleName { get; set; }
[Required]
[MaxLength(4)]
public DateTime ReleaseDate { get; set; }
[Required]
[MaxLength(5)]
public string Test { get; set; }
[Required]
[MaxLength(2)]
public int Wut { get; set; }
}
然後,我有一個自定義的HTML輔助類和表達式擴展類(包括刪節) :
public class InputHelper
{
public static HtmlString Input<T>(Expression<Func<T, Object>> expression, string id, string label)
{
var req = expression.GetAttribute<T, Object, RequiredAttribute>();
var max = expression.GetAttribute<T, Object, MaxLengthAttribute>();
var required = "";
var maxlength = "";
if(req!=null)
{
required = "req";
}
if(max!=null)
{
maxlength = "maxlength='" + max.Length + "'";
}
return new HtmlString("<div class=\"clearfix\"><label for=\""+id+"\">" + label + "</label>" +
"<div class=\"input\"><input id=\""+id+"\" class=\""+required+"\" type=\"text\" "+maxlength+"/></div></div>");
}
}
public static class ExpressionExtensions
{
public static TAttribute GetAttribute<TIn, TOut, TAttribute>(this Expression<Func<TIn, TOut>> expression) where TAttribute : Attribute
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression != null)
{
var attributes = memberExpression.Member.GetCustomAttributes(typeof(TAttribute), true);
return attributes.Length > 0 ? attributes[0] as TAttribute : null;
}
return null;
}
}
在我的剃鬚刀劇本,我提出以下電話:
@(InputHelper.Input<string>(m => Model.Title.TitleName, "titlename", "Title Name"))
@(InputHelper.Input<string>(m => Model.Title.Test, "testfield", "Test Field"))
@(InputHelper.Input<int>(m => Model.Title.Wut, "tester", "Test Field 2"))
@(InputHelper.Input<DateTime>(m => Model.Title.ReleaseDate, "release_year", "Release Year"))
出於某種原因,T他的GetAttribute方法只找到TitleName和Test的屬性,兩者都是TitleCreateModel的字符串屬性。它無法找到ReleaseDate和Wut的屬性,我不知道爲什麼。
這工作,但我不知道它爲什麼。有關於可以鏈接到的表達式的好資源嗎? –