AttributeUsage属性 - 指定属性继承规则

指定属性继承规则

AttributeUsage的最后一个参数是继承标志,指出速航行是否可以被继承。如果设为true,它的意义取决于AllowMultiple属性的值。

继承

AllowMultiple

结果

True

False

派生的属性覆盖基属性

True

True

派生的属性和基属性共存


None.gif using  System;
None.gif
using  System.Reflection;
None.gif
None.gif
namespace  AttribInheritance
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [AttributeUsage(
InBlock.gif        AttributeTargets.All,
InBlock.gif        
//AllowMultiple=true,
InBlock.gif
        AllowMultiple=false,
InBlock.gif        Inherited
=true
InBlock.gif        )]
InBlock.gif        
public class SomethingAttribute : Attribute
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
private string name;
InBlock.gif            
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ name = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
public SomethingAttribute(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.name = str;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        [Something(
"abc")]
InBlock.gif        
class MyClass
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        [Something(
"def")]
InBlock.gif        
class Another : MyClass
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            [STAThread]
InBlock.gif            
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Type type 
= Type.GetType("AttribInheritance.Another");
InBlock.gif                
foreach(Attribute attr in 
InBlock.gif                                type.GetCustomAttributes(
true))
InBlock.gif                                
//type.GetCustomAttributes(false))
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    SomethingAttribute sa 
= attr as SomethingAttribute;
InBlock.gif                    
if(null != sa)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Console.WriteLine(
"Custom Attribute : {0}", sa.Name);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif}

None.gif            
None.gif        
None.gif        
None.gif
None.gif                

当AllowMultiple被设置为false时,结果为:
Custom Attribute : def
当AllowMultiple被设置为true时,结果为:
Custom Attribute : def
Custom Attribute : abc
注意,如果将false传递给GetCustomAttributes,它不会搜索继承树,所以你只能得到派生的类属性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值