如何在运行时加载程序集并创建类实例? [英] How to load Assembly at runtime and create class instance?

查看:25
本文介绍了如何在运行时加载程序集并创建类实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大会.在这个程序集中,我有一个类和接口.我需要在运行时加载这个程序集,想创建一个类的对象,也想使用接口.

I have a assembly. In this assembly I have a class and interface. I need to load this assembly at runtime and want to create an object of the class and also want to use the interface.

Assembly MyDALL = Assembly.Load("DALL"); // DALL is name of my dll
Type MyLoadClass = MyDALL.GetType("DALL.LoadClass"); // LoadClass is my class
object obj = Activator.CreateInstance(MyLoadClass);

这是我的代码.如何改进?

This is my code. How could it be improved?

推荐答案

如果您的程序集在 GAC 或 bin 中,请在类型名称的末尾使用程序集名称而不是 Assembly.Load().

If your assembly is in GAC or bin use the assembly name at the end of type name instead of Assembly.Load().

object obj = Activator.CreateInstance(Type.GetType("DALL.LoadClass, DALL", true));

这篇关于如何在运行时加载程序集并创建类实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆