2

I have to write an add-in for an existing application. Client will use the application and add-in. Both application and add-in are black boxes for the user. My add-in is a .Net C# dll and User Can Code in C#.

Now I have to provide a mechanism through which user can write some C# code. That code will be loaded and executed by my add-in at runtime in order to get data required in add-in.

What is the best practise to achieve this

Edit

Just to clear a bit more...

In my add-in, during execution, I get a string

Now I want to call some user function

string resultString = UserFunction("SomeString");

Now I want to know how to allow user to write this UserFunction so that I can call it at runtime and get resultString back based on SomeString

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
  • Look at both MAF. This question should help you: http://stackoverflow.com/questions/835182/choosing-between-mef-and-maf-system-addin – Jordão Jan 25 '12 at 15:40

2 Answers2

3

Er..this gets a bit tricksy...especially if it has to happen exactly the way you phrased the question.

You can allow the user to pass text c# code to a function in your dll, then basically compile that code into an in-memory assembly, but this seems like a lot of work to allow someone to pass code into an already compiled DLL, in order to be recompiled and ran again.

You can look at the post here for more information on this topic : http://www.dotnetthoughts.net/2011/01/09/how-to-compile-c-code-snippet-in-runtime/

A better practice would be to only pass DATA into the DLL if you could somehow refactor your application to support that.

David C
  • 3,610
  • 3
  • 21
  • 23
1

Check out the answers to a similar question: Is it possible to dynamically compile and execute C# code fragments?. The second answer has a good suggestion regarding using a base class with virtual methods that your contributors override.

Community
  • 1
  • 1
Ed Power
  • 8,310
  • 3
  • 36
  • 42