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.