using System; [Flags] public enum Contribution { Pension = 0x01, ProfitSharing = 0x02, CreditBureau = 0x04, SavingsPlan = 0x08, All = Pension | ProfitSharing | CreditBureau | SavingsPlan } public class Employee { private Contribution prop_contributions; public Contribution contributions { get { return prop_contributions; } set { prop_contributions = value; } } } public class Starter { public static void Main() { Employee bob = new Employee(); bob.contributions = Contribution.ProfitSharing | Contribution.CreditBureau; if ((bob.contributions & Contribution.ProfitSharing)== Contribution.ProfitSharing) { Console.WriteLine("Bob enrolled in profit sharing"); } } }
Illustrates the GetCustomAttributes method
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example17_4.cs illustrates the GetCustomAttributes method */ using System; public class Example17_4 { public static void Main() { // retrieve all attributes of Class1 Console.WriteLine("Class1 attributes: "); object[] aAttributes = Attribute.GetCustomAttributes( typeof(Class1)); foreach (object attr in aAttributes) { Console.WriteLine(attr); } } } // declare an attribute named UnitTest // UnitTest.Written is either true or false public class UnitTest : Attribute { bool bWritten; public bool Written() { return bWritten; } public UnitTest(bool Written) { bWritten = Written; } } // declare another attribute named LifeCycle // LifeCycle.Stage returns a string public class LifeCycle : Attribute { string sStage; public string Stage() { return sStage; } public LifeCycle(string Stage) { sStage = Stage; } } // apply the attribues to a class [UnitTest(true)] [LifeCycle("Coding")] public class Class1 { }
Illustrates use of the Conditional attribute
/* illustrates use of the Conditional attribute */ #define USE_METHOD_1 using System; using System.Diagnostics; public class Example17_2 { [Conditional("USE_METHOD_1")] public static void Method1() { Console.WriteLine("In Method 1"); } public static void Main() { Console.WriteLine("In Main"); Method1(); } }
How to create a custom attribute
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example17_3.cs shows how to create a custom attribute */ using System; public class Example17_3 { public static void Main() { UnitTest u; // retrieve and display the UnitTest attributes of the classes Console.Write("Class1 UnitTest attribute: "); u = (UnitTest) Attribute.GetCustomAttribute( typeof(Class1), typeof(UnitTest)); Console.WriteLine(u.Written()); Console.Write("Class2 UnitTest attribute: "); u = (UnitTest) Attribute.GetCustomAttribute( typeof(Class2), typeof(UnitTest)); Console.WriteLine(u.Written()); } } // declare an attribute named UnitTest // UnitTest.Written is either true or false public class UnitTest : Attribute { bool bWritten; public bool Written() { return bWritten; } public UnitTest(bool Written) { bWritten = Written; } } // apply the UnitTest attribute to two classes [UnitTest(true)] public class Class1 { } [UnitTest(false)] public class Class2 { }
Shows the use of assembly attributes
/* shows the use of assembly attributes */ using System; using System.Reflection; using System.Windows.Forms; [assembly:AssemblyVersionAttribute("1.0.0.0")] [assembly:AssemblyTitleAttribute("Example 16.1")] public class Example16_1 { string privateString; public string inString { get { return privateString; } set { privateString = inString; } } public void upper(out string upperString) { upperString = privateString.ToUpper(); } public static void Main() { } }
Compiles into a library defining the RamdomSupplier attribute and the RandomMethod attribute
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example17_5a compiles into a library defining the RamdomSupplier attribute and the RandomMethod attribute */ using System; // declare an attribute named RandomSupplier [AttributeUsage(AttributeTargets.Class)] public class RandomSupplier : Attribute { public RandomSupplier() { // doesn't have to do anything // we just use this attribute to mark selected classes } } // declare an attribute named RandomMethod [AttributeUsage(AttributeTargets.Method )] public class RandomMethod : Attribute { public RandomMethod() { // doesn't have to do anything // we just use this attribute to mark selected methods } } //=================================================== /* Example17_5b implements one class to supply random numbers */ // flag the class as a random supplier [RandomSupplier] public class OriginalRandom { [RandomMethod] public int GetRandom() { return 5; } } //=================================================== /* Example17_5c implements one class to supply random numbers */ using System; // flag the class as a random supplier [RandomSupplier] public class NewRandom { [RandomMethod] public int ImprovedRandom() { Random r = new Random(); return r.Next(1, 100); } } // this class has nothing to do with random numbers public class AnotherClass { public int NotRandom() { return 1; } } //=================================================== /* Example17_5d illustrates runtime type discovery */ using System; using System.Reflection; class Example17_5d { public static void Main(string[] args) { RandomSupplier rs; RandomMethod rm; // iterate over all command-line arguments foreach(string s in args) { Assembly a = Assembly.LoadFrom(s); // Look through all the types in the assembly foreach(Type t in a.GetTypes()) { rs = (RandomSupplier) Attribute.GetCustomAttribute( t, typeof(RandomSupplier)); if(rs != null) { Console.WriteLine("Found RandomSupplier class {0} in {1}", t, s); foreach(MethodInfo m in t.GetMethods()) { rm = (RandomMethod) Attribute.GetCustomAttribute( m, typeof(RandomMethod)); if(rm != null) { Console.WriteLine("Found RandomMethod method {0}" , m.Name ); } } } } } } }
Illustrates use of the Obsolete attribute
/* illustrates use of the Obsolete attribute */ using System; public class Example17_1 { // warn the user that Method1 is obsolete [Obsolete("Method1 has been replaced by NewMethod1", false)] public static int Method1() { return 1; } // throw an error if the user tries to use Method2 [Obsolete("Method2 has been replaced by NewMethod2", true)] public static int Method2() { return 2; } public static void Main() { Console.WriteLine(Method1()); Console.WriteLine(Method2()); } }