博客
关于我
隔空取物——reflection
阅读量:759 次
发布时间:2019-03-23

本文共 1860 字,大约阅读时间需要 6 分钟。

在C#中,通过反射操作已加载的程序集中的类型和方法,可以实现隔空操作。以下是关于使用反射从外部DLL获取信息并调用方法的步骤指南:

1. 创建项目

首先,创建一个新的C#项目,选择“空项目”模板,展开项目属性,确保“输出类型”设置为“类库”,这样可以避免生成控制台应用程序,防止缺少Main方法的问题。

2. 设置输出路径

在项目属性中,将“输出路径”设置为反射所需的目录,确保反射可以找到生成的DLL文件。

3. 预先准备DLL文件

确保测试反射的下层项目(TestDll)已在bin/Debug目录下正确生成TestDll.dll文件。如果遇到生成问题,检查项目输出路径是否正确。

4. 配置反射代码

在反射使用的程序中,使用以下代码加载DLL并获取所需的类型。确保在代码中正确引用System.Reflection命名空间,或者在代码开头添加using System.Reflection;。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Reflection;namespace Reflection{    class Program    {        static void Main(string[] args)        {            // 加载所需的DLL文件            string assemblyPath = Environment.CurrentDirectory + "\\TestDll.dll";            Assembly loadedDll = Assembly.LoadFile(assemblyPath);            try            {                // 获取类                Type mathType = loadedDll.GetType("TestDll.Math");                // 获取方法                MethodInfo addMethod = mathType.GetMethod("Add");                // 调用方法并处理结果                object result = addMethod.Invoke(new object[] {10, 20});                Console.WriteLine("Method result: " + result);            }            catch (Exception ex)            {                Console.WriteLine("Exception: " + ex.Message);            }            finally            {                Console.WriteLine("Reflection completed.");            }        }    }}

5. 注意事项和误区

  • 命名空间配置:确保TestDll项目的命名空间设置正确,否则反射无法找到TestDll.Math类。可以在项目的Properties中找到AnnealingSupported的快捷键设置,确保全名的正确性。
  • 大写问题:类名TestDll.Math的首字母是否大写会影响反射结果。在代码中使用 GetType("TestDll.Math")而不是GetType("testDll.math")。
  • 权限检查:如果反射从本外部加载DLL,确保 Odibi集中的类和方法的访问权限正确。有时可能需要在代码中加入反射权限 року//"反射权限。

6. 测试反射结果

编写上述代码后,运行反射程序,查看输出结果是否正确调用了TestDll.Math.Add方法。如果出现错误,回头检查类名、命名空间和方法定义是否正确。

通过以上步骤,可以名 正确地反射外部类和方法,实现隔空取物的效果。记住,反射提供了一个灵活的解决方案,尤其是在需要动态获取类型信息或修改代码行为时非常有用。

转载地址:http://tpzzk.baihongyu.com/

你可能感兴趣的文章
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm 下载依赖慢的解决方案(亲测有效)
查看>>