简单的例子。

本文介绍了一个自定义Attribute的实现过程,包括定义自定义Attribute、如何使用它标记类,并通过示例展示了如何在运行时获取这些Attribute的信息。
自定义Attribute
ContractedBlock.gifExpandedBlockStart.gif自定义Attribute
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Relaction.test1
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// 自定义的Attribute类型。
 7InBlock.gif    /// </summary>
 8ExpandedSubBlockEnd.gif    /// 

 9InBlock.gif    [AttributeUsage(AttributeTargets.Class,Inherited=false,AllowMultiple=false)]
10InBlock.gif    public class ClassShowProcessAttribute:Attribute
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12InBlock.gif        private bool _show = false;
13InBlock.gif        public bool Show
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            get
16ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
17InBlock.gif                return _show;
18ExpandedSubBlockEnd.gif            }

19ExpandedSubBlockEnd.gif        }

20InBlock.gif        public ClassShowProcessAttribute(bool show)
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            _show = show;
23ExpandedSubBlockEnd.gif        }

24ExpandedSubBlockEnd.gif    }

25ExpandedBlockEnd.gif}
使用上面的Attribute。
ContractedBlock.gifExpandedBlockStart.gif使用Attribute
 1None.gifusing System;
 2None.gif
 3None.gifnamespace Relaction.test1
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 6InBlock.gif    /// 给这个类贴上自定义的属性。
 7InBlock.gif    /// </summary>
 8ExpandedSubBlockEnd.gif    /// 

 9InBlock.gif    [ClassShowProcess(false)]
10InBlock.gif    public class AttrClassTest
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12InBlock.gif        public AttrClassTest()
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
14ExpandedSubBlockEnd.gif    }

15ExpandedBlockEnd.gif}

16None.gif
应用测试:
ContractedBlock.gifExpandedBlockStart.gif应用测试
 1None.gif    private void button2_Click(object sender, System.EventArgs e)
 2ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 3ExpandedSubBlockStart.gifContractedSubBlock.gif            object[] o = new object[]dot.gif{new AttrClassTest(),new AttrClassTest(),"TEST"};
 4InBlock.gif            foreach(object item in o)
 5ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 6InBlock.gif                ClassShowProcessAttribute[] attr = (ClassShowProcessAttribute[])item.GetType().GetCustomAttributes(typeof(ClassShowProcessAttribute),false);
 7InBlock.gif                if(attr.Length != 0)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 9InBlock.gif                    if(attr[0].Show)
10ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
11InBlock.gif                        label1.Text += item.ToString();
12ExpandedSubBlockEnd.gif                    }

13ExpandedSubBlockEnd.gif                }

14ExpandedSubBlockEnd.gif            }

15ExpandedBlockEnd.gif        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值