コード例 #1
0
        public static void AttributesMethod()
        {
            //attribute is a class that inherit of Attribute class.
            //One function of attibutes is tag methods, classes, types and so on, in order to query down tagged members in  reflection.
            //you can create properties in an attribute (must have get; set; statements) and assign values when you tag members
            //you can create a constructor in an attribute and assign parameters when you tag members
            //you can make restrictions in attributes (to use just in clases, delegates, methods ans so on)


            //1. Get the assembly
            Assembly assembly = Assembly.GetExecutingAssembly();

            // select all the types which have at least one tag or attribute with myCustomAttribute
            var myCustomAttributeQuery = from t in assembly.GetTypes()
                                         where t.GetCustomAttributes <myCustomAttribute>().Count() > 0
                                         select t;

            foreach (var type in myCustomAttributeQuery)
            {
                Console.WriteLine(type.Name);
                //Get all properties tagged with myCustomAttribute

                var properties = from property in type.GetProperties()
                                 where property.GetCustomAttributes <myCustomAttribute>().Count() > 0
                                 select property;

                foreach (PropertyInfo property in properties)
                {
                    Console.WriteLine(property.Name);
                }

                var methods = from method in type.GetMethods()
                              where method.GetCustomAttributes <myCustomAttribute>().Count() > 0
                              select method;

                foreach (MethodInfo method in methods)
                {
                    Console.WriteLine(method.Name);
                }
            }

            //Getting the property values of a custom Attribute
            //1. get the type of the tagged class and of the attibute
            Type TransportationType = typeof(transportation);
            Type customAttrType     = typeof(CustomAttr);

            //2.execute the GetCustomAttribute method from the abstract class Attribute
            CustomAttr ca = (CustomAttr)Attribute.GetCustomAttribute(TransportationType, customAttrType);

            Console.WriteLine("{0}, {1}", ca.antiguity, ca.medium);

            //You can create a constructor in an attribute an  inicialize in when you tag the member
        }
コード例 #2
0
        public OrderComparer()
        {
            Type objType = typeof(T);

            foreach (PropertyInfo oneProp in objType.GetProperties())
            {
                CustomAttr customOrder = (CustomAttr)oneProp.GetCustomAttribute(typeof(CustomAttr), true);
                if (customOrder == null)
                {
                    continue;
                }
                sortOrder.Add(customOrder.Order, oneProp);
            }
        }