Ejemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            this.ListInfo.Items.Add("Вывод информации о сборке:");
            Assembly i = Assembly.GetExecutingAssembly();

            this.ListInfo.Items.Add("Полное имя:" + i.FullName);
            this.ListInfo.Items.Add("Исполняемый файл:" + i.Location);
            this.ListInfo.Items.Add("");
            this.ListInfo.Items.Add("");
            Reflection.ForInspection obj = new Reflection.ForInspection();
            Type File = obj.GetType();

            //другой способ
            //Type t = typeof(ForInspection);
            this.ListInfo.Items.Add("\nИнформация о типе:");
            this.ListInfo.Items.Add("Тип " + File.FullName + " унаследован от " + File.BaseType.FullName);
            this.ListInfo.Items.Add("Пространство имен " + File.Namespace);
            this.ListInfo.Items.Add("Находится в сборке " + File.AssemblyQualifiedName);
            this.ListInfo.Items.Add("");
            this.ListInfo.Items.Add("\nКонструкторы:");
            foreach (var x in File.GetConstructors())
            {
                this.ListInfo.Items.Add(x);
            }
            this.ListInfo.Items.Add("");
            this.ListInfo.Items.Add("\nМетоды:");
            foreach (var x in File.GetMethods())
            {
                this.ListInfo.Items.Add(x);
            }
            this.ListInfo.Items.Add("");
            this.ListInfo.Items.Add("\nСвойства:");
            foreach (var x in File.GetProperties())
            {
                this.ListInfo.Items.Add(x);
            }
            this.ListInfo.Items.Add("");
            this.ListInfo.Items.Add("\nПоля данных (public):");
            foreach (var x in File.GetFields())
            {
                this.ListInfo.Items.Add(x);
            }
            this.ListInfo.Items.Add("");
            this.ListInfo.Items.Add("\nForInspection реализует IComparable -> " + File.GetInterfaces().Contains(typeof(IComparable))
                                    );
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            PlusOrMinus PlusOrMinusParam = (x, y) => { Console.WriteLine("Разность {0} и {1}: {2}", x, y, x - y); };
            int         i1 = 2, i2 = 3;

            Console.WriteLine("С использованием делегатного метода");
            PlusOrMinusMethod(i1, i2, Plus);
            Console.WriteLine("С использованием лямбда-выражения");
            PlusOrMinusMethod(i1, i2, (x, y) => { Console.WriteLine("Разность {0} и {1}: {2}", x, y, x - y); });
            Console.WriteLine("С использованием Action");
            PlusOrMinusMethodAct(i1, i2, Minus);
            Reflection.ForInspection obj = new Reflection.ForInspection();
            Type t = obj.GetType();

            Console.WriteLine("\nИнформация о типе:");
            Console.WriteLine("Тип " + t.FullName + " унаследован от " +
                              t.BaseType.FullName);
            Console.WriteLine("Пространство имен " + t.Namespace);
            Console.WriteLine("Находится в сборке " + 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("\nПоля данных (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(Reflection.NewAttribute), out attrObj))
                {
                    Reflection.NewAttribute attr = attrObj as Reflection.NewAttribute;
                    Console.WriteLine(x.Name + " - " + attr.Description);
                }
            }
            Console.WriteLine("\nВызов метода:");
            //Создание объекта
            //ForInspection fi = new ForInspection();
            //Можно создать объект через рефлексию
            Reflection.ForInspection fi =
                (Reflection.ForInspection)t.InvokeMember(
                    null, BindingFlags.CreateInstance,
                    null, null, new object[] { });
            //Параметры вызова метода
            object[] parameters = new object[] { 3, 2 };
            //Вызов метода
            object Result =
                t.InvokeMember("Plus", BindingFlags.InvokeMethod, null, fi, parameters);

            Console.WriteLine("Plus(3,2)={0}", Result);
        }