动态地从外部程序集加载一个类型 [英] Dynamically load a type from an external assembly

查看:101
本文介绍了动态地从外部程序集加载一个类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从托管代码,我该如何在运行时从另一个程序集加载一个托管类型,假设调用代码没有一个静态引用到装配?

From managed code, how do I load a managed type from another assembly at runtime, assuming the calling code does not have a static reference to the assembly?

要澄清一下,假设我有类库在编译成Lib.dll Lib.cs。我想写一个叫Foo.dll单独的程序一类Foo,不具有参考静态Lib.dll,而是加载Lib.dll,然后为类库的存在反映了,然后调用的方法就可以了。

To clarify, let's say I have class Lib in Lib.cs compiled into Lib.dll. I want to write a class Foo in a separate assembly called Foo.dll, that does not have a reference to Lib.dll statically, but instead loads Lib.dll and then reflects on for the presence of the class Lib and then calls a method on it.

对不起,在这样的反思一个明显的问题。我想它会需要少得多的时间来得到一个论坛,读几篇文章的答案。

Sorry for such an obvious question on Reflection. I figure it'll take much lesser time to get the answer on a forum that to read a few articles.

推荐答案

下面是其中一小段:

Assembly assembly = Assembly.LoadFile("Lib.dll");
ILib lib = (ILib)assembly.CreateInstance("Lib");
lib.doSomething();



当然的lib类必须实现ILIB接口,它必须是可访问无论从Foo.dll和lib .dll文件。这样,您就实现抽象。

Of course the Lib class must implement the ILib interface which must be accessible both from Foo.dll and Lib.dll. This way you achieve abstraction.

这篇关于动态地从外部程序集加载一个类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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