1

I am writing an application that allows the user to create custom algorithms for computing values over a collection of objects. Simply put, i will be having a string with the source code of class with one method.

The solution I have implemented is to compile the string source code in a separate dll for each such custom algorithm and then load them using Assembly.Load and instantiate the class saved in the dll. From a maintainability point of views, this means that i have to store the source code in the db (for example) and also manage the existence of the compiled dlls (recreate by compiling again the source code if it is missing)

Is there a better way to do this, considering the new features of .Net 4.0?

EDIT: The input source code is C# and i am using CSharpCodeProvider to compile the code. The custom classes are all derived from a base class and they override the method that actually holds the computation logic. What i would really like to do is to get rid of the dll management and not lose (too much) performance in compiling all the classes every time my application starts up

2 Answers 2

2

I would look at scripting languages; IronPython is easy to embed, or there are JavaScript engines for .NET. Simple, and usually fast enough.


If (comments) you need to use c#, I would:

  • build all the current methods at the same time into one assembly; solves a lot of problems
  • if the data changes during execution, make use of AppDomains so that I can unload them

I've done something similar where the model/rules were XML, running it through a transform to get c#, and compiling with CSharpCodeProvider (or whatever); and simply polling every minute or so to see if a new build is required

Sign up to request clarification or add additional context in comments.

5 Comments

The requirement is that the source code the user is providing is C#.
And I think you can compile directly into a in-memory assembly. So you don't need to manage them on disk.
@CodeInChaos. Yes, i can compile in memory, but i will have to do this every time the application will start up, for all my classes. I did not do performance testing to see exactly how much time this will take, but when having a lot of custom code, it may not be acceptable to compile every time
@anchandra what I did in my case was persist the compiled dlls to a store, and distribute (pub/sub) out to all the servers, IIRC
Do you really have that much code to compile at runtime? The compiler is relatively fast. I'd compile all classes you want into a single dll on startup and only look into dll management if that becomes too slow in practice. And my instinct is that it's fast enough.
1

The CSharpCodeProvider has been around for a while and should fit the ticket. It can be used to generate the separate libraries like you have been doing (perhaps you are using the CSharpCodeProvider), but it can also be used to generate dynamic class objects. If they all implement an interface you can cast the objects as an interface or you can use reflection to invoke your logic. Here is a codeproject article to achieve something similar:

http://www.codeproject.com/KB/dotnet/dynacodgen.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.