开发者

c# compile source code from database

开发者 https://www.devze.com 2023-01-07 15:11 出处:网络
I would like to build an application framework that is mainly interpreted. Say that the source code would be stored in the database that could be edited by the users andalways the latest version would

I would like to build an application framework that is mainly interpreted. Say that the source code would be stored in the database that could be edited by the users and always the latest version would be executed.

Can anyone give me some ideas how does one implement sth like this !

开发者_运维技巧cheers, gabor


In .Net, you can use reflection and CodeDOM to compile code on the fly. But neither approach is really very simple or practical. Mono has some ability to interpret c# on the fly as well, but I haven't looked closely at it yet.

Another alternative is to go with an interpreted .Net language like Boo or IronPython as the language for your database code.

Either way, make sure you think long and hard about the security of your platform. Allowing users to execute arbitrary code is always an exercise fraught with peril. It's often too tempting to look for a simple eval() method, and even if one exists, that is not good enough for this kind of scenario.


Try Mono ( http://www.monoproject.org ). It supports many scripting languages including JavaScript.


If you don't want to use any scripting you can use CodeDOM or Reflection (see Reflection.Emit).

Here are really useful links on the topic :

  • Dynamically executing code in .Net (Here you can find a tool which can be very helpul)
  • Late Binding and On-the-Fly Code Generation Using Reflection in C#
  • Dynamic Source Code Generation and Compilation


Usually the Program uses a scripting language for the scriptable parts, i.e. Lua or Javascript.


To answer your technical question: You don't want to write your own language and interpreter. That's too much work for you to do. So pick some other language, say Python or Lua, and look for the documentation that lets your C program hand it blocks of code to execute. Of course, the script needs to be able to do something, so you'll need to find how to expose your program's objects to the script. Also, what will happen if a client is running the program when you update its source code in the database? Should the client restart? Are you going to store the entire program as a single row in this database, or did you want to store individual functions? That affects how you structure your updates.

To address other issues with your question: Why do you want to do this? Making "interpreted language" part of your design spec for a system is not often a good sign. Is the real requirement something like this: "I update the program often and I want users to always have the latest copy?" If so, there are other, better ways to go about this (just give us your actual scenario and requirements).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号