zoukankan      html  css  js  c++  java
  • 反射实例【转】

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    //必须得加的命名空间
    using System.Reflection;

    namespace 反射举例
    {
        public class Program
        {
            private String file = @"反射举例.exe";

            static void Main(string[] args)
            {
                Program test = new Program();
                test.DisplayModules();
                test.DisplayTypes();
                test.DisplayMethods();
                test.InvokeStaticMethod();
                test.InvokeMethod();
                Console.ReadLine();
            }
            //显示所有模块名
            public void DisplayModules()
            {
                Console.WriteLine("display modules ...");
                Assembly assembly = Assembly.LoadFrom(file);
                Module[] modules = assembly.GetModules();
                foreach (Module module in modules)
                {
                    Console.WriteLine("module name:" + module.Name);
                }
            }
            //显示所有类型名
            public void DisplayTypes()
            {
                Console.WriteLine("display types ...");
                Assembly assembly = Assembly.LoadFrom(file);
                Type[] types = assembly.GetTypes();
                foreach (Type type in types)
                {
                    Console.WriteLine("type name:" + type.FullName);
                }
            }
            //先是一个类型中的所有方法名
            public void DisplayMethods()
            {
                Console.WriteLine("display methods in TestReflection Type ...");
                Assembly assembly = Assembly.LoadFrom(file);
                Type type = assembly.GetType("反射举例.Program");
                MethodInfo[] methods = type.GetMethods();
                foreach (MethodInfo method in methods)
                {
                    Console.WriteLine("method name:" + method.Name);
                }
            }
            //调用一个类中的静态方法
            public void InvokeStaticMethod()
            {
                Console.WriteLine("Invoke static method ...");
                Assembly assembly = Assembly.LoadFrom(file);
                Type type = assembly.GetType("反射举例.TestSubClass");
                type.InvokeMember("SayHello", BindingFlags.InvokeMethod, null, null, new object[] { });
            }
            //调用一个类中的非静态方法
            public void InvokeMethod()
            {
                Console.WriteLine("Invoke Method ...");
                Assembly assembly = Assembly.LoadFrom(file);
                Type type = assembly.GetType("反射举例.TestSubClass");
                Object obj = Activator.CreateInstance(type);
                TestClass tc = (TestClass)obj;
                type.InvokeMember("Test1", BindingFlags.InvokeMethod, null, tc, new object[] { });
                type.InvokeMember("Test2", BindingFlags.InvokeMethod, null, tc, new object[] { });
            }
        }
        public interface TestClass
        {
            void Test1();
            void Test2();
        }
        public class TestSubClass : TestClass
        {
            public void Test1()
            {
                Console.WriteLine("This is TestSubClass.Test1");
            }
            public void Test2()
            {
                Console.WriteLine("This is TestSubClass.Test2");
            }
            public static void SayHello()
            {
                Console.WriteLine("This is TestSubClass.SayHello");
            }
        }

    }

  • 相关阅读:
    Ubuntu打开终端的方法三种
    javascript 获取随机数
    HTML5中类jQuery选择器querySelector的使用
    PHP stream_context_create()作用和用法分析
    一些常用的api接口、
    怎么样学好C++
    指针访问与数组访问的效率分析
    架构师
    Java 之 StringTokenizer
    类型转换操作符static_cast、const_cast、dynamic_cast、reinterpret_cast
  • 原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/3328618.html
Copyright © 2011-2022 走看看