从完整类型名称加载程序集

本文关键字:加载 程序集 类型 | 更新日期: 2023-09-27 18:03:35

我有一个库TestProject1。名称空间- TestProject1。我在这儿上课——Class1。我可以加载它从另一个项目使用完整的类名(TestProject1.Class1)?(dll在另一个项目的文件夹中)。我想看到解决方案,而不是在文件夹中抛出所有"*.dll"文件。

只能动态加载

从完整类型名称加载程序集

是的,由于您的项目中有此另一个程序集的引用,因此您可以从类型创建assembly对象并获取该类型的一些属性,例如:

Assembly asm = Assembly.GetAssembly(typeof(Class1));
string fullName = asm.FullName;

如果您需要加载未被引用到项目中的程序集,您可以使用Assembly.LoadFile方法,传递dll的路径,例如:

var asm = System.Reflection.Assembly.LoadFile("C:''project.dll");
// Get the type to use.
Type class1 = asm.GetType("Class1");
// Get the method to call.
MethodInfo myMethod = class1.GetMethod("MethodA");
// Create an instance. 
object obj = Activator.CreateInstance(class1);

asm汇编对象一起工作,您将没有强类型代码,但您可以使用Reflection来使用该汇编的类型。

记得包含反射命名空间:

using System.Reflection;

嗯,我自己的答案是使用

Assembly.LoadWithPatrialName("TestProject1.Class1");

我说的对吗?