This example shows
-reading attributes
-initialize object in dependence of attributes value
-reading attributes from class and from properties
-early binding / late binding examples
-iteration through types
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using CarLib; using Numbers; using System.Reflection; namespace AttrPracticeMain { class Program { static void Main(string[] args) { ReflectOnAttributesUsingEarlyBinding(); // early binding EvenNumbersWithIteration(); // late binding NotEvenNumbers(); // late binding EvenNumbers(); // late binding EvenNotEvenOrBoth(); // late binding EvenOddOrBothToProps(); //late binding Console.ReadLine(); } static void EvenNumbersWithIteration() { // get types Assembly asm = Assembly.LoadFile("C:\\C#\\MyC#StudyProjects\\08_Attributes\\Practice\\AttrPracticeMain\\AttrPracticeMain\\bin\\Debug\\Numbers.dll"); Type TCustomAttribute = asm.GetType("Numbers.CustomAttribute"); PropertyInfo PropertyDescription = TCustomAttribute.GetProperty("Description"); Type[] types = asm.GetTypes(); //iterate through types foreach (Type t in types) { object[] objs = t.GetCustomAttributes(TCustomAttribute, false); foreach (object o in objs) { if ((t.Name == "EvenNumbers") && (PropertyDescription.GetValue(o, null).ToString() == "EvenNumbersClass")) { // Console.WriteLine("Catched"); //Create instance and fill array with Evens object EvenNumbersObject = Activator.CreateInstance(t); (EvenNumbersObject as EvenNumbers).Content = new int[] { 2, 4, 6 }; Console.WriteLine("Success even numbers with iteration"); } } } } static void EvenNumbers() { Assembly asm = Assembly.LoadFile("C:\\C#\\MyC#StudyProjects\\08_Attributes\\Practice\\AttrPracticeMain\\AttrPracticeMain\\bin\\Debug\\Numbers.dll"); Type TCustomAttribute = asm.GetType("Numbers.CustomAttribute"); Type TEvenNumbers=asm.GetType("Numbers.EvenNumbers"); PropertyInfo PropertyDescription = TCustomAttribute.GetProperty("Description"); var myCustomAttribute= TEvenNumbers.GetCustomAttribute(TCustomAttribute); if (PropertyDescription.GetValue(myCustomAttribute, null).ToString() == "EvenNumbersClass") { // create instance and fill array object EvenNumbersObject = Activator.CreateInstance(TEvenNumbers); (EvenNumbersObject as EvenNumbers).Content = new int[] { 2, 4, 6 }; Console.WriteLine("Success with Even Numbers"); } } static void NotEvenNumbers() { Assembly asm = Assembly.LoadFile("C:\\C#\\MyC#StudyProjects\\08_Attributes\\Practice\\AttrPracticeMain\\AttrPracticeMain\\bin\\Debug\\Numbers.dll"); Type TCustomAttribute = asm.GetType("Numbers.CustomAttribute"); Type TNotEvenNumbers = asm.GetType("Numbers.NotEvenNumbers"); PropertyInfo pi = TCustomAttribute.GetProperty("Description"); var myCustomAttribute = TNotEvenNumbers.GetCustomAttribute(TCustomAttribute); if (pi.GetValue(myCustomAttribute, null).ToString() == "NotEvenNumbersClass") { // create instance and fill array object EvenNumbersObject = Activator.CreateInstance(TNotEvenNumbers); (EvenNumbersObject as NotEvenNumbers).Content = new int[] { 1, 3, 5 }; Console.WriteLine("Success with non Even Numbers"); } } static void EvenNotEvenOrBoth() { Assembly asm = Assembly.LoadFile("C:\\C#\\MyC#StudyProjects\\08_Attributes\\Practice\\AttrPracticeMain\\AttrPracticeMain\\bin\\Debug\\Numbers.dll"); Type TCustomAttribute = asm.GetType("Numbers.CustomAttribute"); Type TAllNumbers = asm.GetType("Numbers.AllNumbers"); PropertyInfo pi = TCustomAttribute.GetProperty("Description"); var myCustomAttribute = TAllNumbers.GetCustomAttribute(TCustomAttribute); if (pi.GetValue(myCustomAttribute, null).ToString() == "EvenNumbers") { // create instance and fill array object EvenNumbersObject = Activator.CreateInstance(TAllNumbers); (EvenNumbersObject as AllNumbers).Content = new int[] { 2, 4, 6 }; } else if (pi.GetValue(myCustomAttribute, null).ToString() == "NotEvenNumbers") { // create instance and fill array object EvenNumbersObject = Activator.CreateInstance(TAllNumbers); (EvenNumbersObject as AllNumbers).Content = new int[] { 1, 3, 5 }; } else if (pi.GetValue(myCustomAttribute, null).ToString() == "EvenAndUnEvenNumbers") { // create instance and fill array object NumbersObject = Activator.CreateInstance(TAllNumbers); (NumbersObject as AllNumbers).Content = new int[] { 1, 2, 3, 4, 5, 6 }; } Console. WriteLine("Success with non Even Numbers. Current option is "+ (pi.GetValue(myCustomAttribute, null).ToString())); } // read attributes early binding private static void ReflectOnAttributesUsingEarlyBinding() { Type t = typeof(SportsCar); // Knowing type SportsCar, so early binding object[] customAtts = t.GetCustomAttributes(false); foreach (Object o in customAtts) { if (o is CustomAttributeDescription) Console.WriteLine("->{0}\n", (o as CustomAttributeDescription).Description); } } //--------------- static void EvenOddOrBothToProps() { // load and define types Assembly asm = Assembly.LoadFile("C:\\C#\\MyC#StudyProjects\\08_Attributes\\Practice\\AttrPracticeMain\\AttrPracticeMain\\bin\\Debug\\Numbers.dll"); Type TCustomAttribute = asm.GetType("Numbers.CustomAttribute"); Type TAllNumbers = asm.GetType("Numbers.AllNumbers"); PropertyInfo pi = TCustomAttribute.GetProperty("Description"); var myCustomAttribute = TAllNumbers.GetCustomAttribute(TCustomAttribute); //creating instance object EvenNumbersObject = Activator.CreateInstance(TAllNumbers); // reading props and filling arrays PropertyInfo[] piArray = TAllNumbers.GetProperties(); foreach (PropertyInfo p in piArray) { var propAttr = p.GetCustomAttribute(TCustomAttribute); //if (propAttr!=null) continue; if ((propAttr != null)&&(pi.GetValue(propAttr, null).ToString() == "Even")) { (EvenNumbersObject as AllNumbers).IntArray = new int[] { 2, 4, 6 }; } if ((propAttr != null)&&(pi.GetValue(propAttr, null).ToString() == "Odd")) { // create instance and fill with evens (EvenNumbersObject as AllNumbers).AnotherIntArray = new int[] { 1, 3, 5 }; } } //check foreach (int i in (EvenNumbersObject as AllNumbers).IntArray) Console.WriteLine(i); Console.WriteLine(" "); foreach (int i in (EvenNumbersObject as AllNumbers).AnotherIntArray) Console.WriteLine(i); } } } |
Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Numbers { // defining Custom Attribute class public sealed class CustomAttribute : System.Attribute { public string Description { get; set; } public CustomAttribute(string ADescription) { Description = ADescription; } public CustomAttribute() { } } public abstract class Numbers { public int[] Content { get; set; } } [CustomAttribute("EvenNumbersClass")] public class EvenNumbers:Numbers { } [CustomAttribute("NotEvenNumbersClass")] public class NotEvenNumbers:Numbers { } [CustomAttribute("EvenNumbers")] public class AllNumbers : Numbers { } } |
CarLib
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarLib { // defining Custom Attribute class public sealed class CustomAttributeDescription : System.Attribute { public string Description { get; set; } public CustomAttributeDescription(string ADescription) { Description = ADescription; } public CustomAttributeDescription() { } } [Serializable, CustomAttributeDescription("BaseClass")] public abstract class Car { public int MaxSpeed { get; set; } public abstract void TurboBoost(); } [Serializable, CustomAttributeDescription("It is attribute description.There is Descendent of Car")] public class SportsCar : Car { public SportsCar() { MaxSpeed = 160; } public override void TurboBoost() { Console.WriteLine(MaxSpeed+" km/h"); } } } |