Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.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# 是否将System.ComponentModel.DisplayName与动态值一起使用?_C#_Asp.net Mvc_System.componentmodel - Fatal编程技术网

C# 是否将System.ComponentModel.DisplayName与动态值一起使用?

C# 是否将System.ComponentModel.DisplayName与动态值一起使用?,c#,asp.net-mvc,system.componentmodel,C#,Asp.net Mvc,System.componentmodel,我试图在我的系统中实现多语言支持, 其他正在工作的系统使用从数据库生成的XML文件来实现这一点,他们已经使用了一段时间,所以他们希望我也使用它 我已经设法翻译了除formmodels中的显示名以外的所有内容,这些值显然只能是常量值,因此我无法使用获得正确翻译的方法 代码现在是这样的: [System.ComponentModel.DisplayName("Kontraktnamn")] public string Name { get; set; } 我想这样做: [System.Compon

我试图在我的系统中实现多语言支持, 其他正在工作的系统使用从数据库生成的XML文件来实现这一点,他们已经使用了一段时间,所以他们希望我也使用它

我已经设法翻译了除formmodels中的显示名以外的所有内容,这些值显然只能是常量值,因此我无法使用获得正确翻译的方法

代码现在是这样的:

[System.ComponentModel.DisplayName("Kontraktnamn")]
public string Name { get; set; }
我想这样做:

[System.ComponentModel.DisplayName(GetTextByKey("Contract_Name"))]
public string Name { get; set; }

有可能解决这个问题吗?或者,有更好的方法可以做到这一点,并且仍然使用xmlfiles?

您需要创建自己的自定义属性来读取xml值:

public class CustomDisplayName : DisplayNameAttribute
{
    public CustomDisplayName()
    {
        this.DisplayName = MyXmlReader.Read(DisplayName);
    }
}

您需要创建自己的自定义属性来读取xml值:

public class CustomDisplayName : DisplayNameAttribute
{
    public CustomDisplayName()
    {
        this.DisplayName = MyXmlReader.Read(DisplayName);
    }
}

未在我的系统上编译,因为无法设置DisplayName。但是您可以这样做
public CustomDisplayName(aDisplayName):base(aDisplayName){}覆盖字符串DisplayName{get{return MyXmlReader.Read(base.DisplayName);}}
在我的系统上没有编译,因为无法设置DisplayName。但是您可以这样做
publiccustomdisplayname(aDisplayName):base(aDisplayName){}覆盖字符串DisplayName{get{返回MyXmlReader.Read(base.DisplayName);}}