开发者

How can I create an instance of an object defined in my App.Config without having a reference to the assembly in my project?

I am continuing on from a previous question relating to loading instances of plugins specified in the app.config.

I'm curious if it's possible to add a plugin without requiring a reference added to my project and if so, how do I go about that?

<configuration>
  <appSettings>
    <add key="Plugin" value="Prototypes.BensPlugin, PrototypePlugins" />
  </appSettings>
</configuration>

So now I want to load my plugin using:

string tn = /* Retrieve from config */
Type t = Type.GetType(tn);
IPlugin plugin = (IPlugin)Activator.CreateInstance(t);

Given the nature of a plugin, I'm thinking that my assembly shouldn't require a reference to it. It seems like Type.GetType() requires that the plugin assembly is in the reference list (or the GAC?) w开发者_C百科hich leads me to believe that this is not the right approach to plugging in features.

Is this possible or am I looking in the wrong direction?


Basically, adding this line of code

System.Reflection.Assembly.Load("PrototypePlugins");

before the Type.GetType(tn); would do the job providing that PrototypePlugins.dll is in the same directory that the current executing program. But, note that you could not unload that assembly.

A better approach is to load that assembly in another AppDomain so you can unload it by killing the AppDomain. But this is not trivial so I would suggest you to use an IoC container like this one : http://www.castleproject.org/container/

Manitra.


You need to load the assembly with Assembly.Load() and then look for the right Type object in the assembly's types which you get by assembly.GetTypes().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜