Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I want commit the cardinal sin of storing code in a database field then evaluate it in my code behind. i.e.

C#
object.current != null && object.current.id != 0


This code will differ depending on the current record. I'm using C# .net and want to determine if this statement is true/false then have some else display accordingly in the code behind. Is this possible? Only other developers will have access to the code records.

Thanks in advance.

Eric
Posted
Comments
AspDotNetDev 14-Oct-11 18:10pm    
Just to clarify, you want to store C# code in a SQL database and then later load that code and execute it from C#?
Sergey Alexandrovich Kryukov 15-Oct-11 0:00am    
Yes, by this sample it looks like C#. Well, I described nearly all what's involved in my solution -- please see.
--SA
ehwash 17-Oct-11 9:26am    
It seems from my limited experience both the suggested solutions may work, but, are overly complicated for my current simple needs. Thanks for the answers. I'll try to come up with something easier for my needs. Thanks

-ew

It is possible to compile code in running time, Its quite complicated. I think you would find it helpful: Link[^]

I don't know why you would like You probably could save the code as a DLL file and then download it, and use it instead (no need to compile in run-time)
 
Share this answer
 
v3
Comments
Mehdi Gholam 14-Oct-11 23:43pm    
My 5!
Sergey Alexandrovich Kryukov 15-Oct-11 0:26am    
What is described in this article is not really complicate. Another thing is much more difficult: working with AppDomain is a must to have compiled assemblies re-loaded. I explained it in my solution, please see.
--SA
BillWoodruff 15-Oct-11 20:57pm    
+5 Good answer that responds clearly to what the user asks !
ehwash 17-Oct-11 9:26am    
It seems from my limited experience both the suggested solutions may work, but, are overly complicated for my current simple needs. Thanks for the answers. I'll try to come up with something easier for my needs. Thanks

-ew
You certainly can store the code in database, but running it will be way more difficult. To run code dynamically, you need to compile it, which is quite possible through CodeDOM. It actually works through regular C# compiler, and this compiler is always bundled with all versions of .NET and run by CodeDOM.

Now, this is how the problem looks like:

You cannot compile one expression. The minimal code you can have is one class with one method. You don't have to store it all in the database, but I suggest that a stored code would be a whole function, otherwise it's hard go make the code compile, validate it, etc. You can develop a system which implements some interface, and the stored function would be implementation. On top of that, you can add some pre-created code to this implementation, so the stored function will be inserted in it. However, you can store whole C# file, which is a bit easier.

Now, you should have code which compiles this C# file in memory, shows compile-time error, etc. Second phase would be checking up if the required interface is implemented. Using Reflection, you can do that and also create the object of a class implementing the interface and invoke it and return the result of calculation.

Now, will it be a surprise for you: what I just described was the easiest part! Now we're getting to a difficult part:

You cannot do it in the same application domain as the one of your main program. Here is why: the compiled assembly will be loaded in this application domain, but .NET does not allow unloading of any assemblies, for some really good reasons. You can only unload a whole process or Application Domain, nothing else. So, there is one solution: compile your assembly in memory in a separate Application Domain. When you need to compile another assembly, you will unload that Application Domain and create a new one, as this is the only way to unload dynamically compiled assembly. But now, remember that the Application Domains are well isolated, as well as separate processes. What does it mean to you? It means that you will have a problem calling the compiled method, because you need to pass parameter values to it and return the value. You will have to do it through the boundary between Application Domain, so it will require Inter-Process Communications (IPC). To simplify this, the class System.AppDomain already has simplified IPC facilities in it. In particular, you can use System.AppDomain.DoCallback. See:
http://msdn.microsoft.com/en-us/library/system.appdomain.aspx[^],
http://msdn.microsoft.com/en-us/library/system.appdomain.docallback.aspx[^].

I described related problems and some architectural skeletons in my past solutions. Please see:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^],
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^] (this one is the closest to your problem).

There are some alternatives:

1) Compile assembly into an executable file, a console application with command line interface and returning value either via ExitCode or in a text form, output onto console. A parent application can run it using System.Diagnostics.Process.Run and re-direct the console output.
See:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^] (here you will find a sample of re-direction).
For command line parser, you can you use my work published in CodeProject: Enumeration-based Command Line Utility[^] — this library is quite convenient and easy to use.

2) Develop a fully-fledged parser from some simple language. The result of parsing should be an expression tree capable of substitution of some primitive-type values and evaluation of expression. I mean it: your stored expressions will not be in C#, but is some simplistic language which would allow to parse and evaluate single expressions. You will probably able to use some available code for this.

So, now think about it all and decide is you really want to get into all this.


—SA
 
Share this answer
 
v2
Comments
Mehdi Gholam 15-Oct-11 0:21am    
Complete! My 5!
Sergey Alexandrovich Kryukov 15-Oct-11 0:24am    
Thank you, Mehdi.
--SA
RaisKazi 15-Oct-11 2:22am    
My 5! I doubt, if OP will get more comprehensive answer than this.
Sergey Alexandrovich Kryukov 15-Oct-11 2:29am    
Thank you very much, Rais. This is because I've done some 3 projects involving all that. Of course, I did not want to store any code in database but solved much more fundamental problems.
--SA
Simon Bang Terkildsen 15-Oct-11 18:11pm    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900