使用非const字符串作为类别属性名称
本文关键字:属性 const 字符串 | 更新日期: 2023-09-27 18:05:22
所有,我正在本地化我的c#应用程序,我已经来到我的设置控件。当我使用PropertyGrid
时,我使用属性来提供选项。我有一个CategoryAttribute
的"服务器访问",我需要在不同的语言显示-我有:
[CategoryAttribute("ServerAccess"),
ReadOnlyAttribute(false),
XmlElement,
DescriptionAttribute("Message about the Server Access Option.")]
public bool ShowSystemDatabases
{
get { return this.showSystemDatabases; }
set { this.showSystemDatabases = value; }
}
,我试图将其更改为:
[CategoryAttribute(ObjectStrings.SettingsServerAccess),
ReadOnlyAttribute(false),
XmlElement,
DescriptionAttribute(MessageStrings.SettingsShowSystemDatabaseInfo)]
public bool ShowSystemDatabases
{
get { return this.showSystemDatabases; }
set { this.showSystemDatabases = value; }
}
其中ObjectStrings
是我的资源文件('ObjectStrings。Resx等人)。这会抛出一个编译时错误
An attribute argument must be a constant expression, typeof expression or array
creation expression of an attribute parameter type...
显然,当构造函数期望const string
时,我不能使用字符串。我试过用各种圆屋方法从string
铸造到const string
,但都失败了。这是一个很简单的问题,但是……
解决这个问题最简单的方法是什么?
感谢您的宝贵时间。
我不知道如何处理这个问题,特别是在控件属性的情况下,但实现这一点的一般方法是在运行时解析资源,在属性上使用如下参数:
MessageResourceType = typeof (MyResource), MessageResourceName = "MyResourceKey")
而不是直接传递指针到资源键。
我不知道是否有一个等效的方式为CategoryAttribute, DescriptionAttribute和其他控件的属性属性,或者如果有一种方法来重载它们