• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
编写人生
写写代码,写写人生
博客园    首页    新随笔    联系   管理    订阅  订阅
让你的控件属性注释支持多语言
我们知道在开发控件时,可以为某个属性添加DescriptionAttribute标记,就可以在属性栏中显示他的注释,像下面这样:
        private int _qua;
        [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 的实现。
    [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;
            }

        }

    }

太简单,太巧妙了,他重载了Description的Get,改从资源文件中获取。
就这么简单。
posted on 2005-11-01 18:46  编写人生  阅读(2662)  评论(3)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3