C# asp.net mvc 5 t4脚手架
我使用vs2013和T4模板生成视图(创建、编辑、列表) 我需要的是检索Maxlength属性 Create.cs.t4文件我有以下代码(代码段)C# asp.net mvc 5 t4脚手架,c#,asp.net-mvc,templates,t4,C#,Asp.net Mvc,Templates,T4,我使用vs2013和T4模板生成视图(创建、编辑、列表) 我需要的是检索Maxlength属性 Create.cs.t4文件我有以下代码(代码段) 最大长度为 字符串MaxLength(PropertyInfo属性){ var stringLength=property.GetCustomAttributes(typeof(StringLengthAttribute),false); if(stringLength!=null&&stringLength.Length>0){ 返回((Stri
最大长度为
字符串MaxLength(PropertyInfo属性){
var stringLength=property.GetCustomAttributes(typeof(StringLengthAttribute),false);
if(stringLength!=null&&stringLength.Length>0){
返回((StringLengthAttribute)stringLength[0]).MaximumLength.ToString();
}否则{
返回“0”;
}
}
我知道我要的是PropertyMetadata而不是PropertyInfo,但这是我能得到的最接近的数据,但它没有给我任何回报
不知道转换PropertyMetadata>PropertyInfo是否有效,但不知道
我的目标是查看MaxLength=1的属性
注意:模板的结构与VS2013 VS2012不同,因此我无法理解。读取MVC5 T4模板中属性的解决方案相当长,因此我在这里写了一篇博文:我有一个非常类似的问题,你是否碰巧发现了这个问题?还没有,我试着找到解决办法有人找到解决办法了吗?
<#
foreach (PropertyMetadata property in ModelMetadata.Properties) {
string inputlength = MaxLength(property);
#>
<#= property.PropertyName #> Maxlength is <#= inputlength #>
<# } #>
string MaxLength(PropertyInfo property) {
var stringLength = property.GetCustomAttributes(typeof(StringLengthAttribute), false);
if (stringLength != null && stringLength.Length > 0) {
return ((StringLengthAttribute)stringLength[0]).MaximumLength.ToString();
} else {
return "0";
}
}