简体   繁体   English

使用非常量字符串作为CategoryAttribute名称

[英]Using a Non-Const String as a CategoryAttribute Name

All, I am localising my C# application and I have come to my settings control. 全部,我正在本地化C#应用程序,并且进入了设置控件。 As I am using a PropertyGrid I am using properties to provide the options. 当我使用PropertyGrid我正在使用属性来提供选项。 I have a CategoryAttribute for "Server Access" I need to show in different languages - I had: 我需要使用不同的语言来显示“服务器访问”的CategoryAttribute我有:

[CategoryAttribute("ServerAccess"),
 ReadOnlyAttribute(false),
 XmlElement,
 DescriptionAttribute("Message about the Server Access Option.")]
public bool ShowSystemDatabases 
{
    get { return this.showSystemDatabases; }
    set { this.showSystemDatabases = value; }
}

and I attempted to change this to: 我试图将其更改为:

[CategoryAttribute(ObjectStrings.SettingsServerAccess),
 ReadOnlyAttribute(false),
 XmlElement,
 DescriptionAttribute(MessageStrings.SettingsShowSystemDatabaseInfo)]
public bool ShowSystemDatabases 
{
    get { return this.showSystemDatabases; }
    set { this.showSystemDatabases = value; }
}

Where ObjectStrings is my resource file ('ObjectStrings.resx' et al.). 其中ObjectStrings是我的资源文件(“ ObjectStrings.resx”等)。 This is throwing a compile-time error 这会引发编译时错误

An attribute argument must be a constant expression, typeof expression or array 
creation expression of an attribute parameter type...

clearly I can't use a string when the constructor is expecting a const string . 显然,当构造函数需要const string时,我不能使用const string I have tried casting from string to const string using various round-the-houses method but all have failed. 我尝试使用各种舍入方法从stringconst string但都失败了。 it is a simple enough issue, but... 这是一个非常简单的问题,但是...

What is the easiest way around this problem? 解决此问题的最简单方法是什么?

Thanks for your time. 谢谢你的时间。

I don't know how you could handle this especially in the case of control's attributes, but a general way to achieve this is to make Resources resolved at runtime, using parameters like these on your attribute : 我不知道如何处理这个问题,尤其是在控件具有属性的情况下,但是实现此目的的一般方法是在运行时使用诸如此类的参数来解析资源:

MessageResourceType = typeof (MyResource), MessageResourceName = "MyResourceKey")

instead of directy passing the pointer to resource key. 而不是直接将指针传递给资源密钥。

I don't know if there is an equivalent way for CategoryAttribute, DescriptionAttribute and others Controls' properties attributes, or if there is a way to overload them. 我不知道是否存在等效的CategoryAttribute,DescriptionAttribute和其他控件的属性属性的方法,或者是否有重载它们的方法。

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

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