Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NET中的用户友好属性名称,如Rails中的属性名称_C#_.net_Asp.net Mvc - Fatal编程技术网

C# NET中的用户友好属性名称,如Rails中的属性名称

C# NET中的用户友好属性名称,如Rails中的属性名称,c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,在RubyonRails中,配置中有一个YAML文件,允许您定义模型属性名的纯英语版本。实际上,它允许您定义任何语言版本:它是国际化的一部分,但大多数人使用它来向用户显示模型验证结果 我在.NETMVC4项目中需要这种功能。用户提交一个表单,并收到一封几乎所有他们发布内容的电子邮件(表单绑定到一个模型)。我编写了一个助手方法,通过反射转储属性/值对的HTML表,例如 foreach (PropertyInfo info in obj.GetType() .GetProperties(Bi

在RubyonRails中,配置中有一个YAML文件,允许您定义模型属性名的纯英语版本。实际上,它允许您定义任何语言版本:它是国际化的一部分,但大多数人使用它来向用户显示模型验证结果

我在.NETMVC4项目中需要这种功能。用户提交一个表单,并收到一封几乎所有他们发布内容的电子邮件(表单绑定到一个模型)。我编写了一个助手方法,通过反射转储属性/值对的HTML表,例如

foreach (PropertyInfo info in obj.GetType()
    .GetProperties(BindingFlags.Public | 
                   BindingFlags.Instance | 
                   BindingFlags.IgnoreCase)) 
{
  if (info.CanRead && !PropertyNamesToExclude.Contains(info.Name)) 
  {
    string value = info.GetValue(obj, null) != null ? 
                                            info.GetValue(obj, null).ToString() :
                                            null;
    html += "<tr><th>" + info.Name + "</th><td>" + value + "</td></tr>";
  }
}
foreach(obj.GetType()中的PropertyInfo信息)
.GetProperties(BindingFlags.Public)
BindingFlags.Instance|
BindingFlags.IgnoreCase)
{
if(info.CanRead&!PropertyNamesToExclude.Contains(info.Name))
{
字符串值=info.GetValue(obj,null)!=null?
info.GetValue(obj,null).ToString():
无效的
html+=“”+info.Name+“”+value+“”;
}
}

但当然,这会打印出
info.Name
,就像“ordergid”,而“orderusername”可能会更好。在.NET中有类似的内容吗?

有一个名为DisplayName的数据属性,允许您执行此操作。只需使用此属性和每个属性的友好名称对模型属性进行注释

[DisplayName("Full name")]
public string FullName { get; set; }

非常感谢@Stokedout和@Clemens的回答。实际上,通过反射进行访问有点复杂。由于某些原因,我无法直接访问CustomAttributes属性。最后得出结论:

DisplayNameAttribute dna = (DisplayNameAttribute)info
    .GetCustomAttributes(typeof(DisplayNameAttribute), true).FirstOrDefault();

string name = dna != null ? dna.DisplayName : info.Name;

string value = info.GetValue(obj, null) != null ? 
     (info.GetValue(obj, null).GetType().IsArray ? 
           String.Join(", ", info.GetValue(obj, null) as string[]) : 
           info.GetValue(obj, null).ToString()) : 
      null;

html += "<tr><th>" + name + "</th><td>" + value + "</td></tr>";
DisplayNameAttribute dna=(DisplayNameAttribute)信息
.GetCustomAttributes(typeof(DisplayNameAttribute),true).FirstOrDefault();
字符串名称=dna!=无效的dna.DisplayName:info.Name;
字符串值=info.GetValue(obj,null)!=无效的
(info.GetValue(obj,null).GetType().IsArray?
String.Join(“,”,info.GetValue(obj,null)作为字符串[]):
info.GetValue(obj,null).ToString()):
无效的
html+=“”+名称+“”+值+“”;

如果您需要通过反射(如问题中所要求的)访问该属性,您将在PropertyInfo.+1的属性中找到它。如果您需要更多数据,您始终可以创建自定义属性,为每个字段添加更多(可选)元数据。值得添加
@Html.DisplayFor()
将语法添加到此答案中,以指示如何轻松访问它。