让你的控件属性注释支持多语言

翻译|其它|编辑:郝浩|2007-09-18 10:07:01.000|阅读 922 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

我们知道在开发控件时,可以为某个属性添加  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,改从资源文件中获取。


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP