属性国际化 [英] Attribute Internationalization
问题描述
我想知道是否存在一种将国际化应用于属性的方法.
I was wondering if there was a way to apply internationalization to attributes.
我有一个从文本文件中检索所有字符串的类,另一个具有属性的类,另一个继承了Description Attribute的类.
I have one class which does all the string retrieval from text files, another class which has properties, and another which inherits Description Attribute.
到目前为止,我已经编写了继承DescriptionAttribute的类:
So far I have written the class that inherits DescriptionAttribute:
[AttributeUsage(AttributeTargets.All)]
[AttributeUsage(AttributeTargets.All)]
公共类testDefaulPropertyAttribute:DescriptionAttribute
public class testDefaulPropertyAttribute : DescriptionAttribute
{
{
私有字符串testVar;
private string testVar;
public testDefaulPropertyAttribute(string testVar):base()
public testDefaulPropertyAttribute(string testVar) : base()
{
{
this.testVar = testVar;
this.testVar = testVar;
}
}
公共替代字符串说明
public override string Description
{
{
获取
get
{
{
if(testVar =="property1")
if (testVar == "property1")
{
{
返回属性1";
return "Property 1";
}
}
else if(testVar =="property2")
else if (testVar == "property2")
{
{
返回属性2";
return "Property 2";
}
}
}
}
}
}
}
}
在具有属性的类中,我做这样的事情:
Inside the class that has the properties, I do something like this:
[testDefaulPropertyAttribute("property1"))
[testDefaulPropertyAttribute("property1")]
公共布尔Prop1
{
get {return prop1; }
get { return prop1; }
设置{prop1 = value; }
set { prop1 = value; }
}
我想知道是否有一种方法可以从从文本文件中检索字符串的类中检索输入字符串到testDefaulPropertyAttribute类中?我不确定如何执行此操作,因为我不知道如何在运行时检索属性输入.
I was wondering if there is a way to retrieve my input string to the testDefaulPropertyAttribute class from my class that retrieves string from text files? I'm not sure how to do this since I don't know how to retrieve attribute input at runtime.
推荐答案
为什么没有多个属性来处理目标语言?您必须在属性返回中做一些工作,但是它是可行的.我在博客上展示了如何管理多个属性值: C#在对象上使用扩展属性信息
Why not have multiple attributes to handle the target language? You have to do some work in the attribute return but its doable. I show how to manage multiple attribute values on my blog: C# Using Extended Attribute Information on Objects
这篇关于属性国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!