示例#1
0
文件: Program.cs 项目: GorkSmorc/Lab
        static void Main(string[] args)
        {
            Cat  mycat = new Cat();
            Type t     = mycat.GetType();

            Console.WriteLine("\nInformation of type");

            Console.WriteLine("Namespace " + t.Namespace);
            Console.WriteLine("Assembly Qualified " + t.AssemblyQualifiedName);
            Console.WriteLine("\nConstructors");
            foreach (var x in t.GetConstructors())
            {
                Console.WriteLine(x);
            }
            Console.WriteLine("\nMethods:");
            foreach (var x in t.GetMethods())
            {
                Console.WriteLine(x);
            }
            Console.WriteLine("\nProperties:");
            foreach (var x in t.GetProperties())
            {
                Console.WriteLine(x);
            }
            Console.WriteLine("\nData (public):");
            foreach (var x in t.GetFields())
            {
                Console.WriteLine(x);
            }

            Console.WriteLine("\nProperties with attributes:");
            foreach (var x in t.GetProperties())
            {
                object attrObj;
                if (GetPropertyAttribute(x, typeof(NewAttribute), out attrObj))
                {
                    NewAttribute attr = attrObj as NewAttribute;
                    Console.WriteLine(x.Name + " - " + attr.Description);
                }
            }

            Console.WriteLine("\nMethod by using reflection");
            Cat myCat = (Cat)t.InvokeMember(null, BindingFlags.CreateInstance, null, null, new object[] { });

            object[] parameters = new object[] { 2, 5 };

            object Result = t.InvokeMember("Cat_moves", BindingFlags.InvokeMethod, null, myCat, parameters);

            Console.WriteLine("Current place " + Result);

            Console.WriteLine("\nPress a key to continue");
            Console.Read();
        }
示例#2
0
        static void Main(string[] args)
        {
            Cat  mycat = new Cat();
            Type t     = mycat.GetType();

            Console.WriteLine("\nИнформация о типе");

            Console.WriteLine("Namespace " + t.Namespace);
            Console.WriteLine("Assembly Qualified " + t.AssemblyQualifiedName);
            Console.WriteLine("\nКонструкторы");
            foreach (var x in t.GetConstructors())
            {
                Console.WriteLine(x);
            }
            Console.WriteLine("\nМетоды:");
            foreach (var x in t.GetMethods())
            {
                Console.WriteLine(x);
            }
            Console.WriteLine("\nСвойства:");
            foreach (var x in t.GetProperties())
            {
                Console.WriteLine(x);
            }
            Console.WriteLine("\nData (public):");
            foreach (var x in t.GetFields())
            {
                Console.WriteLine(x);
            }

            Console.WriteLine("\nАтрибуты свойств:");
            foreach (var x in t.GetProperties())
            {
                object attrObj;
                if (GetPropertyAttribute(x, typeof(NewAttribute), out attrObj))
                {
                    NewAttribute attr = attrObj as NewAttribute;
                    Console.WriteLine(x.Name + " - " + attr.Description);
                }
            }

            Console.WriteLine("\nМетод использования рефлексии");
            Cat myCat = (Cat)t.InvokeMember(null, BindingFlags.CreateInstance, null, null, new object[] { });

            object[] parameters = new object[] { 2, 5 };

            object Result = t.InvokeMember("Cat_moves", BindingFlags.InvokeMethod, null, myCat, parameters);

            Console.WriteLine("Current place " + Result);
            Console.WriteLine("\nНажмите любую кнопку для продолжения");
            Console.Read();
        }