简体   繁体   English

将System.ComponentModel.DisplayName与动态值一起使用?

[英]Using System.ComponentModel.DisplayName with dynamic values?

Im trying to implement multi-language support in my system, the other systems at work uses xmlfiles for this generated from a database that they have used for some time now so they want me to use this aswell. 我试图在我的系统中实现多语言支持,其他工作系统使用xmlfiles来生成它们已经使用了一段时间的数据库,所以他们希望我也使用它。

I have managed to translate everything except the displaynames in my formmodels, these values can apperantly only be constant values so i can't use a method that gets the correct translation. 我已经设法翻译除了我的formmodels中的显示名之外的所有内容,这些值显然只是常量值,所以我不能使用获得正确翻译的方法。

This is how the code is now: 这就是代码现在的样子:

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

And i want to do something like this: 我想做这样的事情:

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

Is it possible to work around this? 有可能解决这个问题吗? Or maybe there is a better way to do it and still use the xmlfiles? 或者也许有更好的方法来做它仍然使用xmlfiles?

You'll need to create your own custom attribute that can read the xml values: 您需要创建自己的可以读取xml值的自定义属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM