Example #1
0
        // The Attribute class also provides a method  called GetCustomAttribute to get an attribute from a particular type.
        public void ReadAnAttribute()
        {
            Attribute           a = Attribute.GetCustomAttribute(typeof(Person), typeof(ProgrammerAttribute));
            ProgrammerAttribute p = (ProgrammerAttribute)a;

            Console.WriteLine("Programmer : {0}", p.Programmer);
        }
        public static void executeTest()
        {
            Attribute a = Attribute.GetCustomAttribute(typeof(Person), typeof(ProgrammerAttribute));

            ProgrammerAttribute p = (ProgrammerAttribute)a;

            Console.WriteLine($"Val variable p {p.Programmer}");
            Console.ReadKey();

            Person pe   = new Person();
            Type   type = pe.GetType();

            foreach (MemberInfo member in type.GetMembers())
            {
                Console.WriteLine($"Members: {member.ToString()}");
            }
            Console.WriteLine();

            //calling methos
            MethodInfo sm = type.GetMethod("set_Name");

            sm.Invoke(pe, new object [] { "Fred" });
            Console.WriteLine(pe.Name);
            Console.ReadKey();
        }
Example #3
0
        /* We know how to add attributes to objects, so now we need to know how the program can read
         * the attributes back. We saw that the Attribute class provides a static method called
         * IsDefined that be used to determine if a class has a particular attribute defined. The
         * Attribute class also provides a method called GetCustomAttribute to get an attribute from
         * a particular type. It has the same parameters as IsDefined (the type of the class and the
         * type of the attribute class) and returns a reference to the attribute. If the attribute
         * is not defined on the class, GetCustomAttribute returns null. In the code below the
         * Attribute returned is cast to a ProgrammerAttribute and the name of the programmer is
         * printed. */
        private static void ReadAnAttributeExample()
        {
            Attribute a = Attribute.GetCustomAttribute(typeof(Person2), typeof(ProgrammerAttribute));

            ProgrammerAttribute p = (ProgrammerAttribute)a;

            Console.WriteLine("Programmer: {0}", p.Programmer);
        }
Example #4
0
        /*
         * //this will use compilation error as we are only allowed to apply this  attribute to classes
         * [ProgrammerAttribute (programmer: "fred")]
         * public string Name { get; set; }
         */


        public static void RunAttributeClass()
        {
            System.Type type;

            Person human = new Person();

            type = human.GetType();
            Console.WriteLine("Person type: {0}", type.ToString());



            #region Calling a method on an object by using reflection

            //set the name of the person by using set_Name of Name property in Person class
            //its finding methodInfo

            MethodInfo setMethod = type.GetMethod("set_Name");

            //calling invoke that is supplied with a referece to the Person that is the target of the method invocation and an array of oobject references -- whic will be used as arguments to that method call
            setMethod.Invoke(human, new object[] { "Fred" });

            Console.WriteLine("\nCalling a method on an object by using reflection");
            Console.WriteLine(human.Name);
            #endregion



            foreach (MemberInfo item in type.GetMembers())
            {
                Console.WriteLine("\nInfo about the Person class:");
                Console.WriteLine(item.ToString());
            }


            Attribute a = Attribute.GetCustomAttribute(typeof(Person), typeof(ProgrammerAttribute));


            ProgrammerAttribute p = (ProgrammerAttribute)a;

            Console.WriteLine("\nProgrammer: {0}", p.Programmer);



            Console.ReadKey();
        }