使用反射(Reflect)获取DLL文件中的类型并调用方法可以分为以下几个步骤:
1.加载DLL文件:使用Assembly.LoadFrom方法加载指定DLL文件。
Assembly assembly = Assembly.LoadFrom("MyLibrary.dll");
2.获取类型:使用Assembly.GetType方法获取指定类型的Type对象。
Type type = assembly.GetType("MyLibrary.MyClass");
3.创建对象:使用Activator.CreateInstance方法创建指定类型的实例。
object instance = Activator.CreateInstance(type);
4.调用方法:使用Type.GetMethod方法获取指定方法的MethodInfo对象,然后使用MethodInfo.Invoke方法调用该方法。
MethodInfo method = type.GetMethod("MyMethod");
method.Invoke(instance, null);
创建对象的两种方法:
Activator.CreateInstance方法可以创建任意类型的对象,使用Type.GetConstructor方法获取到指定的构造函数,然后调用其Invoke方法来创建对象。
using System;
using System.Reflection;
class Program
{
static void Main(strin

获取DLL文件中的类型,创建对象并调用对象的方法。&spm=1001.2101.3001.5002&articleId=134659663&d=1&t=3&u=74622eabe99c448e867a6eda48c31a4c)
539

被折叠的 条评论
为什么被折叠?



