views:

168

answers:

3

As a perl programmer I can evaluate strings as code If I wished, Can I do the same in C#( with strings or some other object containing user input)?

What I want to accomplish is to create an object where its methods may be predefined in my source code or may be defined at run-time by the user by entering a string that represents C# code for a method or a SQL Query. The method call should always return its scalar value as a string, I believe it would be desirable to make available some pre-defined "system" variables for use in the method call and some "Cleanup" code to validate that a string is actually returned.

Psuedo - Structure

 Object Statistic
      string  Name;
      functionref Method;

The architecture I have in mind would basically collect these in realtime and add them upon request of the user to the list of statistics that user wants to display. Defined Statistics could be saved to a file and loaded into the main program during initialization. This way the user doesn't have to keep redefining the desired statistic. Edit/Update/Delete of statistics are needed.

If this is successful then I( the programmer) won't have to go and add new code every time someone decides that they have a new piece of information they want displayed on their stat board that I haven't already written code for .

Any ideas on where to start reading to accomplish this in C#?

The purpose for this capability is a program that displays statistics for a running system/database. Values to watch are not necessarily known at design time, nor how to define the value desired for retrieval. I want to allow the User to define any statistics beyond any I pre-code for the system.

+1  A: 

Check out this earlier question.

However, also consider the stability, scalability and security of your solution. If you allow arbitrary execution, your application isn't going to be fun to operate and support. Setup a very well-delimited sandbox for externally provided code.

Also, don't expect ordinary users (whoever they are) to be able coders.

You might want to offer restricted but more manageable extensibility through e.g. dependency injection instead.

Pontus Gagge
A: 

You can use the CodeDomCompiler to compile C# source during run-time (check this for example).

Groo
Can hosting code access return values from CodeDom code? Can CodeDom access host structures or do they need to be provided as a client copy explicitly? Can I pull these functions in and call them from within parent code? It seems like it may be what I want but these points aren't clear me yet...
Steve
Check this link: http://69.10.233.10/KB/cs/evalcscode.aspx. You can see two things: 1. you can import namespaces to CodeDom using the ReferencedAssemblies collection, 2. Using Reflection (Assembly.CreateInstance, MethodInfo.Invoke) you can instantiate your classes and invoke their methods.
Groo
+1  A: 

I'll echo what Pontus said, especially regarding stability, support costs, and expecting users to write code. I'll add performance to the list as well, since these users are likely to be unaware of performance implications of their code. For these reasons and others, user-facing eval-like constructs are not considered wise in C#, and probably shouldn't be in perl as well.

Instead, look at the System.AddIn namespace in .Net for providing plugin functionality for your application.

Joel Coehoorn