特性的简单使用(Attribute)

1.
using System;
using System.Reflection;

//特性使用
namespace CsDev
{
    public class TransactionableAttribute : Attribute
    {
        public TransactionableAttribute(string a)
        {
            Console.WriteLine("Test:"+a);
        }
    }
    class Attr
    {
        [Transactionable("Foo")]
        public void Foo()
        {
        }

        public void Bar() { }

        [Transactionable("Baz")]
        public void Baz()
        {
        }
    }

    class AttrApp
    {
        public static void Main()
        {
            Type type = Type.GetType("CsDev.Attr");
            foreach(MethodInfo m in type.GetMethods())
            {
                foreach(Attribute attr in m.GetCustomAttributes(false))
                {
                    if (attr is TransactionableAttribute)
                        Console.WriteLine("{0} is Transactionable.",m.Name);
                }
            }

            Console.ReadKey();
        }
    }
}

执行结果:

Test:Foo
Foo is Transactionable.
Test:Baz
Baz is Transactionable.


2.枚举类型中Flags特性使用

using System;

namespace CsStudy
{
    enum e_1 : uint
    {
        bita = 0x01,//0
        bitb = 0x02,//1
        bitc = 0x04,//2
        bitd = 0x08 //3
    }

    [Flags]
    enum e_2 : uint
    {
        bita = 0x01,//0
        bitb = 0x02,//1
        bitc = 0x04,//2
        bitd = 0x08 //3
    }

    class Program
    {
        static void Main()
        {
            e_1 testa = e_1.bitc | e_1.bitd | e_1.bitb;
            e_2 testb = e_2.bitc | e_2.bitd | e_2.bitb;

            Console.WriteLine("testa: {0}\n 包含bitc?{1}", testa.ToString(), testa.HasFlag(e_1.bitc) ? "是" : "否");
            Console.WriteLine("testb: {0}\n 包含bitc?{1}", testb.ToString(), testb.HasFlag(e_2.bitc) ? "是" : "否");

            Console.ReadKey();
        }
    }
}
输出:

testa: 14
 包含bitc?是
testb: bitb, bitc, bitd
 包含bitc?是


3.遍历特性

using System;
using System.Diagnostics;
using System.Reflection;

[assembly: CLSCompliant(true)]
[Serializable]
[DefaultMemberAttribute("Main")]
class App
{
    [Conditional("Debug")]
    [Conditional("Release")]
    public void DoSomething() { }
    public App()
    {
    }
    [CLSCompliant(true)]
    [STAThread]
    public static void Main()
    {
        // Display the type’s name.
        Console.WriteLine("Attributes applied to: {0}", typeof(App));
        // Get and show the set of attributes applied to this type.
        ShowAttributes(typeof(App).GetCustomAttributes(false));
        // Get the set of methods associated with the type.
        MemberInfo[] members = typeof(App).FindMembers(
        MemberTypes.Constructor | MemberTypes.Method,
        BindingFlags.DeclaredOnly | BindingFlags.Instance |
        BindingFlags.Public | BindingFlags.Static,
        Type.FilterName, "*");
        foreach (MemberInfo member in members)
        {
            // Display the type’s member name.
            Console.WriteLine("Attributes applied to: {0}", member.Name);
            // Get and show the set of attributes applied to this member.
            ShowAttributes(member.GetCustomAttributes(false));
        }
        Console.ReadKey();
    }
    public static void ShowAttributes(Object[] attributes)
    {
        foreach (Object attribute in attributes)
        {
            // Display the type of each applied attribute.
            Console.Write(" {0}", attribute.GetType().ToString());
            if (attribute is ConditionalAttribute)
                Console.Write(" ({0})",
                ((ConditionalAttribute)attribute).ConditionString);
            if (attribute is CLSCompliantAttribute)
                Console.Write(" ({0})",
                ((CLSCompliantAttribute)attribute).IsCompliant);
            Console.WriteLine();
        }
        if (attributes.Length == 0)
            Console.WriteLine(" No attributes applied to this target.");
        Console.WriteLine();
    }
}

输出:

Attributes applied to: App
 System.Reflection.DefaultMemberAttribute
 System.SerializableAttribute


Attributes applied to: DoSomething
 System.Diagnostics.ConditionalAttribute (Release)
 System.Diagnostics.ConditionalAttribute (Debug)


Attributes applied to: Main
 System.STAThreadAttribute
 System.CLSCompliantAttribute (True)


Attributes applied to: ShowAttributes
 No attributes applied to this target.


Attributes applied to: .ctor
 No attributes applied to this target.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值