特性的简单使用(Attribute)

发布时间:2017-3-23 16:10:03 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"特性的简单使用(Attribute)",主要涉及到特性的简单使用(Attribute)方面的内容,对于特性的简单使用(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.

上一篇:http://support.microsoft.com/kb/2496029/zh-cn
下一篇:JAVA HASH算法

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款