views:

324

answers:

5

i am making a IRC bot in C#, and i would like to have the feature to load plugins... for example: there would be a text file with some code in it, and then the program would import the contents of that text file AS CODE... into a function... how is this possible?

A: 

You might like to look into the CSharpCodeProvider class. There's some example code in this question.

Other than this, there's the option of precompiling dll's and using late binding to load those at runtime.

Other than all this, you can embed a scripting engine like Lua into your program.

The short answer is, there's all sorts of options available, it depends on exactly what you're trying to achieve as to what solution is best.

In the first two examples, you'd be looking at interface programming, you'd have some interface defined in a second dll that you can compile against, then pull some predetermined name out of the assembly via reflection, and assign it to a variable of the interface type and make calls against that.

I've never used Lua, but the article I linked too should explain how to use it.

Matthew Scharley
A: 

You need to look for dynamic code compilation (see http://www.west-wind.com/presentations/dynamicCode/DynamicCode.htm, http://www.codeguru.com/csharp/csharp/cs_misc/designtechniques/article.php/c10729/, http://www.codeproject.com/KB/cs/Dynamic_Code_Generation.aspx)

Don't you think it would be better to load the plugins in as DLLs through reflection instead though? Dynamic code wouldn't be signed so you have no way of verifying that the code comes from a trusted source and, as such, could be opening up the users of your program to some very malicious code.

Fooberichu
It depends. Sometimes you want dynamic compilation, because the code you are compiling is provided by the user (Some sort of scripting system for instance).
Matthew Scharley
Ah, that's a good point there. When he was saying "plugins" that is what I was envisioning, not necessarily scripting.
Fooberichu
+1  A: 

I suggest either using C#-script or some DLR language like IronPython (I'd prefer the latter). But, as others pointed out, it is unsafe to run any code that your program recieves - a more wise idea would be to define a contract interface, and accept the plugins (implementing that interface) in signed assemblies.

Tamás Szelei
+2  A: 

I'm doing the same thing for a MUD server. This server has both compiled plugins and code plugins like you want to use. I created my own scripting engine using CodeDom, but that was quite a hassle. I ended up switching to CS-Script. So now we are using MEF for compiled plugins and CS-Script for code plugins.

hectorsosajr
Can you expand on why it was a hassle? I rolled my own using CodeDom (Ironically, for a MUD client), and it's pretty simple, but still easy to use, extend and such.
Matthew Scharley
Using CodeDom for simple plugins is braindead easy. However things get a lot more complicated when you are trying to unload and/or replace an already loaded plugin. This mostly involved loading the assemblies in a different appdomain. Anything beyond that was a hassle for me. I wanted to build a MUD not a whole plugin framework, hence my comment.
hectorsosajr
A: 

thankyou all for helping, but im looking to share code between the imported code and the main code, for example, there would be a text file, and on load of the main program, it would open that text file and load it as a function... a function that could share code like public strings and ints and activate other functions a commands and use objects etc etc... is this possible?