Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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# 如何获取资源的价值?(超过1种语言)_C#_Model View Controller_Resources_Propertyinfo - Fatal编程技术网

C# 如何获取资源的价值?(超过1种语言)

C# 如何获取资源的价值?(超过1种语言),c#,model-view-controller,resources,propertyinfo,C#,Model View Controller,Resources,Propertyinfo,我只需要资源的价值。我是这样试的。因为我有这个密码。但是为什么属性的属性总是空的呢?我认为该属性是DisplayName 我错了什么?有没有更好的方法来获取资源的价值?不使用模型?(它是电子邮件内容的一部分。) 在控制器中 private string DisplayName(string value) { try { PropertyInfo property = typeof(FormModel).GetProperty(value)

我只需要资源的价值。我是这样试的。因为我有这个密码。但是为什么属性的属性总是空的呢?我认为该属性是DisplayName

我错了什么?有没有更好的方法来获取资源的价值?不使用模型?(它是电子邮件内容的一部分。)

在控制器中

    private string DisplayName(string value)
    {
      try
      {
         PropertyInfo property = typeof(FormModel).GetProperty(value);
         var attribute = property.GetCustomAttribute(typeof(DisplayNameAttribute), true).Cast<DisplayNameAttribute>().FirstOrDefault();
    
         if (attribute == null)
           return value;
        else
           return attribute.DisplayName;
      }
      catch (Exception ex)
      {
          return value;
       }
    }
    
    private string GetHtmlContent(Dto dto)
    {
        string htmlContent = string.Empty;
    
        htmlContent += $"<h4>{DisplayName("PersonalData")</h4>";
        enz...
    }


解决此问题的简单方法可能会将
DisplayNameAttribute
更改为
DisplayNameAttribute

var attribute = property.GetCustomAttribute(typeof(DisplayAttribute), true).Cast<DisplayAttribute>().FirstOrDefault();
var attribute=property.GetCustomAttribute(typeof(DisplayAttribute),true).Cast().FirstOrDefault();
但这并不是制作多语言网站的好方法


请参见

解决此问题的简单方法可能会将
displayname属性
更改为
displayname属性

var attribute = property.GetCustomAttribute(typeof(DisplayAttribute), true).Cast<DisplayAttribute>().FirstOrDefault();
var attribute=property.GetCustomAttribute(typeof(DisplayAttribute),true).Cast().FirstOrDefault();
但这并不是制作多语言网站的好方法