简体   繁体   English

C#如何添加代码以在运行时执行

[英]C# How to add a code to execute at Runtime

I know this requirement may seem weired to many of you but it is one of my project requirement. 我知道这个要求对你们中的许多人来说似乎很奇怪,但这是我的项目要求之一。 Is it possible to add code in Sp and execute in .Net exe. 是否可以在Sp中添加代码并在.Net exe中执行。 Like on button click i call one SP that SP returns few line of code and then program execute the code. 像在按钮上单击一样,我调用一个SP,该SP返回几行代码,然后程序执行该代码。

Is it possible if yes then how? 如果可以,怎么办?

I am using framework 1.1 我正在使用框架1.1

Searched for 'dynamic c# code execution' and Google came up with this blog from 2002... This might work for you.. 搜索“动态C#代码执行”,然后Google提出了2002年的这个博客。这可能对您有用。

http://www.west-wind.com/presentations/dynamiccode/dynamiccode.htm http://www.west-wind.com/presentations/dynamiccode/dynamiccode.htm

The article refers to System.CodeDom.Compiler to compile a assembly on the fly and then using Reflection to execute that code. 本文引用System.CodeDom.Compiler即时编译程序集,然后使用Reflection执行该代码。 Sounds like fun ;) 听起来很好玩 ;)

EDIT: As DaveL suggested: Is it possible to dynamically compile and execute C# code fragments? 编辑:如DaveL建议: 是否可以动态编译和执行C#代码片段?

If you are talking about compiling TSQL into a table and then running this TSQL later via C#, yes it is possible. 如果您正在谈论将TSQL编译到表中,然后稍后通过C#运行此TSQL,是可以的。 What you will need to do is write a simple parser for TSQL script files (.sql). 您需要做的是为TSQL脚本文件(.sql)编写一个简单的解析器。 The parser will read through the SQL script and at each GO statement, add the TSQL statement into the relevant SQL Server table (The table might have two columns 1. Database to run against/name of process to run against. 2. The query itself.). 解析器将通读SQL脚本,并在每个GO语句处将TSQL语句添加到相关的SQL Server表中(该表可能有两列1.要运行的数据库/要运行的进程的名称。2.查询本身)。 You can then execute these TSQL blocks at dynamically at runtime. 然后,您可以在运行时动态地执行这些TSQL块。

If you are talking about running C# that was compiled into an SQL Server data table, this is a different ball game and I am not aware of an easy way to do this (it it is possible at all). 如果您正在谈论运行已编译到SQL Server数据表中的C#,那么这是另一回事,而且我不知道执行此操作的简单方法(这完全有可能)。

I hope this helps. 我希望这有帮助。

Another possibility: 另一种可能性:

By using some binary data type like varbinary in your database, you could store Assemblies in a table as binary data. 通过在数据库中使用诸如varbinary类的二进制数据类型,可以将程序集存储为表中的二进制数据。 You could then read that binary data from the Database, store it locally, load the Assembly via Assembly.LoadFrom() , and then use reflection to find some special type or entry point which you could use. 然后,您可以从数据库中读取该二进制数据,将其存储在本地,通过Assembly.LoadFrom()加载Assembly,然后使用反射来找到一些可以使用的特殊类型或入口点。

You're probably wanting the DLR (where the D stands for Dynamic), unfortunately this is .NET 4.0 upwards. 您可能需要DLR(D代表动态),不幸的是,这是.NET 4.0或更高版本。

Sounds like you're in an awful legacy code netherworld; 听起来您处在糟糕的旧代码环境中; rather you than me. 而不是你,而不是我。

You could create your own scripting language to run within your app but it would be slow and not ideal. 您可以创建自己的脚本语言以在您的应用程序中运行,但这会很慢并且不理想。 Something like YACC would get you part the way there but it's a bumpy ride (having seen somebody attempt this kind of behaviour in Java). 诸如YACC之类的东西会让您脱颖而出,但这是一个坎bump的旅程(已经看到有人在Java中尝试这种行为)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM