让你的控件属性注释支持多语言
来源:互联网 发布:摄影师收入 知乎 编辑:程序博客网 时间:2024/06/05 16:19
我们知道在开发控件时,可以为某个属性添加DescriptionAttribute标记,就可以在属性栏中显示他的注释,像下面这样:
但你会发现,注释的字符串是中文文本写死的,如果我希望控件在英文的环境下显示英文的注释应该怎么办呢?.NET Framework就可以显示不同语言的注释,他是怎么解决的呢?
反编译.NET Framework,我们发现他的注释并没有DescriptionAttribute,而是使用了SRDescriptionAttribute ,例如:
太简单,太巧妙了,他重载了Description的Get,改从资源文件中获取。
就这么简单。
private int _qua;
[Description("此订单明细的数量")]
public int Qua {
get { return _qua; }
set { _qua = value; }
}
[Description("此订单明细的数量")]
public int Qua {
get { return _qua; }
set { _qua = value; }
}
但你会发现,注释的字符串是中文文本写死的,如果我希望控件在英文的环境下显示英文的注释应该怎么办呢?.NET Framework就可以显示不同语言的注释,他是怎么解决的呢?
反编译.NET Framework,我们发现他的注释并没有DescriptionAttribute,而是使用了SRDescriptionAttribute ,例如:
[SRDescription("ControlBottomDescr")]
public int Bottom {
get {
return (this.y + this.height);
}
}
在注释中,.NET Framework没有的确没有直接写英文注释,而是写了一个资源关键字,再查看SRDescriptionAttribute 的实现。public int Bottom {
get {
return (this.y + this.height);
}
}
[AttributeUsage(AttributeTargets.All)]
internal sealed class SRDescriptionAttribute : DescriptionAttribute {
private bool replaced;
public SRDescriptionAttribute(string description)
: base(description) {
}
public override string Description {
get {
if (!this.replaced) {
this.replaced = true;
base.DescriptionValue = SR.GetString(base.Description);
}
return base.Description;
}
}
}
internal sealed class SRDescriptionAttribute : DescriptionAttribute {
private bool replaced;
public SRDescriptionAttribute(string description)
: base(description) {
}
public override string Description {
get {
if (!this.replaced) {
this.replaced = true;
base.DescriptionValue = SR.GetString(base.Description);
}
return base.Description;
}
}
}
太简单,太巧妙了,他重载了Description的Get,改从资源文件中获取。
就这么简单。
- 让你的控件属性注释支持多语言
- 如何让你的iPhone程序支持多语言环境
- 如何让你的iPhone程序支持多语言环境
- 如何让你的iPhone程序支持多语言环境
- 如何让你的iPhone程序支持多语言环境
- 让你的Chrome App支持多语言(i18n)
- 如何让你的iPhone程序支持多语言环境(本地化)
- 如何让你的iPhone程序支持多语言环境(本地化)
- 如何让你的iPhone程序支持多语言环境(本地化)
- 如何让你的iPhone程序支持多语言环境(本地化)
- 如何让你的iPhone程序支持多语言环境(本地化)
- 让你的JLabel支持显示多行文本
- 让你的程序支持插件
- 如何让你的网站支持SSL
- 让你的软件支持繁体中文
- 让你的软件支持繁体中文
- 让你的软件支持繁体中文
- 让你的工程支持MFC
- Asp.Net2.0无刷新客户端回调
- [组图]WebSphere技术专家沙龙活动(上海站)圆满结束
- 一步一步学RMAN第四篇 RMAN备份演练进阶篇
- 远程攻击经典解析
- JMail自定义邮件头信息的BUG
- 让你的控件属性注释支持多语言
- Linux kernel & module program
- 使HTM页面像ASP页面一样接受参数!
- 对于历史数据问题的思考
- Linux那些事儿 之 戏说USB(8)最终奥义
- makefile的写法
- 软件公司创业完全手册[一]
- 一份详尽的IPC$入侵资料
- 通过Internet访问 SharePoint