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

我们知道在开发控件时,可以为某个属性添加 DescriptionAttribute标记,就可以在属性栏中显示他的注释,像下面这样:
None.gif          private   int  _qua;
None.gif        [Description(
" 此订单明细的数量 " )]
ExpandedBlockStart.gifContractedBlock.gif        
public   int  Qua  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _qua; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _qua = value; }
ExpandedBlockEnd.gif        }

None.gif

但你会发现,注释的字符串是中文文本写死的,如果我希望控件在英文的环境下显示英文的注释应该怎么办呢?.NET Framework就可以显示不同语言的注释,他是怎么解决的呢?

反编译.NET Framework,我们发现他的注释并没有 DescriptionAttribute,而是使用了S RDescriptionAttribute ,例如:
None.gif         [SRDescription( " ControlBottomDescr " )]
ExpandedBlockStart.gifContractedBlock.gif        
public   int  Bottom  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{
InBlock.gif                
return (this.y + this.height);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }
在注释中,.NET Framework没有的确没有直接写英文注释,而是写了一个资源关键字,再查看 SRDescriptionAttribute 的实现。
None.gif     [AttributeUsage(AttributeTargets.All)]
ExpandedBlockStart.gifContractedBlock.gif    
internal   sealed   class  SRDescriptionAttribute : DescriptionAttribute  dot.gif {
InBlock.gif        
private bool replaced;
InBlock.gif
InBlock.gif        
public SRDescriptionAttribute(string description)
ExpandedSubBlockStart.gifContractedSubBlock.gif            : 
base(description) dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public override string Description dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (!this.replaced) dot.gif{
InBlock.gif                    
this.replaced = true;
InBlock.gif                    
base.DescriptionValue = SR.GetString(base.Description);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return base.Description;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

太简单,太巧妙了,他重载了Description的Get,改从资源文件中获取。
就这么简单。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值