zoukankan      html  css  js  c++  java
  • 反射——如何使用反射

    //步骤
    //1 加载dll文件
    //2 获取dll文件中的类型
    //3 操作

    #region 通过反射获取所有的类型
                //string filePath = @"C:lvC#练习1Reflectionlab1inDebuglab1.dll";
                //Assembly asm = Assembly.LoadFile(filePath);
                //Type[] typeItem = asm.GetTypes();
                //foreach (Type item in typeItem)
                //{
                //    Console.WriteLine(item.Name);
                //}
                #endregion
    
                #region 通过反射获取某一个类型
                //Type typeItem = asm.GetType("lab1.person");
                //MethodInfo[] method = typeItem.GetMethods();
                //foreach (MethodInfo item in method)
                //{
                //    Console.WriteLine(item.Name);
                //}
                #endregion
    
                #region 通过反射调用类中的方法
                //步骤
                //1 加载程序集
                //2 获取类类型
                //3 获取函数
                //4 调用方法
                string filePath = @"C:lvC#练习1Reflectionlab1inDebuglab1.dll";
                Assembly sem = Assembly.LoadFile(filePath);
                Type typeItem = sem.GetType("lab1.person");
                MethodInfo method = typeItem.GetMethod("SayHi");
                object obj = Activator.CreateInstance(typeItem);
                method.Invoke(obj, null);
                #endregion
    
                Console.ReadKey();
    View Code
  • 相关阅读:
    谷哥的小弟学后台(29)——动态代理
    HDU
    jni 入门 android的C编程之旅 --->环境搭建&&helloworld
    C# 开发系列(二)
    C# 开发系列(一)
    ajax跨域实现api 接口调用
    dependency injection(2)
    要读的书单
    dependency injection
    php dependency innjection
  • 原文地址:https://www.cnblogs.com/lv-sally/p/4727321.html
Copyright © 2011-2022 走看看