有没有一种方法可以动态执行字符串在.NET中,类似的eval()的JavaScript或动态SQL的SQL? [英] Is there a way to dynamically execute a string in .net, similar to eval() in javascript or dynamic sql in sql?

查看:345
本文介绍了有没有一种方法可以动态执行字符串在.NET中,类似的eval()的JavaScript或动态SQL的SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法动态地执行使用.NET 2.0中包含一个字符串,code,以类似的方式对eval()在JavaScript或TSQL使用sp_executesql?

Is there a way to dynamically execute code contained in a string using .net 2.0, in a similar way to eval() in javascript or using sp_executeSQL in tsql?

我有一个变量的字符串值,我想操纵在我的应用程序某些时候 - 这样的code将基本上是字符串操作。我不知道什么不同的操作将需要,所以我想他们是可配置的。

I have a string value in a variable that I want to manipulate at some point in my application - so the code would essentially be string manipulation. I don't know what different manipulations will be needed so i'd like them to be configurable.

我真的不关心什么语言动态code写的,什么是最容易实现的,并足够简单写。

I don't really care what language the dynamic code is written in, whatever is easiest to implement and simple enough to write.

例如我可能要替换的实例。性格与' - ',或者去掉所有空格,或类似的。如果我在做这在SQL中我会使用动态SQL,但我想在.net中code执行它,是这样的:

For example I might want to replace instances of '.' character with '-', or strip out all spaces, or similar. If I was doing this in sql I'd use dynamic sql, but I want to execute it in .net code, something like this:

// Get the value to be manipulated
string s = ... // wherever s comes from

// Get the manipulation code, eg this might come from a database 
// setting that can be changed without recompiling the .net code.
string manipulation = Settings.GetSomeValue("ManipulationSetting");

// This is what I want to know how to do: apply some manipulation to the string.
string result = MagicDynamicEvalClass.Eval(manipulation, s);

// Now I would do stuff with the result.

我可能只使用正则表达式查找/替换EX pressions。因为我做的是字符串操作,这应该是足够提供我能写出聪明足够的常规EX pressions。例如:

I could possibly just use regex find/replace expressions. Since all i'm doing is string manipulation this should be sufficient provided I can write clever enough regular expressions. eg:

// Get the value to be manipulated
string s = ... // wherever s comes from

// Get the code to use to manipulate s, eg this might come from a database 
// setting that can be changed without recompiling the .net code.
string findRegex = Settings.GetSomeValue("RegexPattern");
string replaceRegex = Settings.GetSomeValue("RegexReplace");

// This is what I want to know how to do: apply some manipulation to the string.
string result = Regex.Replace(s, findRegex, replaceRegex);

// Now I can do stuff with the result.

但在某些情况下,我的操作要求可能会超出什么是可能与正则表达式,或者我可能想申请多个步骤,如更换'。用' - '和还剥离空格。也许我可以存储查找列表/替换正则表达式和循环访问他们......但任何人有更好的建议?

But in some cases my manipulation requirement might exceed what's possible with regex, or I might want to apply multiple steps, eg replace '.' with '-' and also strip spaces. Perhaps I could store a list of find/replace regexes and iterate over them... but anyone have a better suggestion?

更新 - 使用动态SQL的例子

UPDATE - example using dynamic sql

我不想要一个解决方案,需要我预先知道哪些操作是可能的,而且我真的很简单的东西。例如,在SQL我会做这样的事情:

I don't want a solution that requires me to know in advance what manipulations are possible, and I'm really looking for something simple. eg in sql I'd do something like this:

declare @s nvarchar(1000)
declare @manipulation nvarchar(1000)
declare @result nvarchar(1000)

-- ... Get the values from wherever they come from

-- Execute the manipulation dynamically
EXEC sp_ExecuteSQL @stmt = @manipulation
    , @params = N'@s nvarchar(1000), @result nvarchar(1000) OUTPUT'
    , @s = @s, @result = @result OUTPUT

然后我可以把任意的SQL到我@manipulation,这样的事情   SET @result = REPLACE(REPLACE(@s,。' - '),'','')

Then I could put arbitrary sql into my @manipulation, something like this SET @result = REPLACE( REPLACE( @s, '.', '-'), ' ', '' )

是的,这需要我要小心允许什么样的价值观付诸@manipulation,但它给我我需要在今后的灵活性。

Yes, this would require me to be careful about what values are allowed to be put into @manipulation, but it would give me the flexibility I need in future.

有一个类似的方法将有可能在JavaScript我猜想,使用eval()。

A similar approach would be possible in javascript I guess, using eval().

更新 - 使用从.NET MSScript控制例如:

UPDATE - example using MSScript control from .net:

似乎是一个可行的方法,但也许矫枉过正简单的情况下,我要处理。它使用Microsoft脚本控制库允许执行任意的VBScript。

This seems like a possible approach, although perhaps overkill for the simple case I want to deal with. It uses the Microsoft Script Control library to allow execution of arbitrary VBScript.

推荐答案

像其他人已经提到它不是真的有可能编译的eval()函数的C#。该功能被刨了它安德斯全世界展示在PDC的CLR后者释放。

like the others already mentioned its not really possible to compile c# in an eval() function. that functionality is planed for a latter release of the clr which anders demoed at the PDC.

作为不同势solutionm,如果你的应用程序能够在单声道运行,你可以只使用其eval函数,可以动态地编译C#code,就像JavaScript的。它basicly已经做什么.NET将能够做到在一年或两年。

as a diffrent solutionm, if your application is able to run on mono you can just use its eval function which can dynamicly compile c# code, just like javascript. it is basicly already doing what .net will be able to do in a year or two.

作为选择,如果你不能使用单声道,你可以写一个执行字符串操作的IronRuby的一部分具有的eval()。您的code不会的其余部分甚至不知道你正在使用红宝石该类/ assambly。

as an alternative if you cant use mono you could write the part that does the string manipulation in ironruby which has eval(). the rest of your code wont even know you are using ruby for that class/assambly.

您张贴在更新的链接看起来pretty的复杂,这样一个简单的用例。使用IronRuby的所有你所要做的就是写MyDynamicEvalClass是这样的:

the link you posted in the update looks pretty complicated for such a simple use case. using ironruby all you would have to do is write the MyDynamicEvalClass something like this:

class MyDynamicEvalClass
  def eval(someString,transformString)
    eval(someString,transformString)
  end
end

和一些红宝石code,它返回一个新字符串替换ManipulationSetting

and replacing "ManipulationSetting" with some ruby code that returns a new string

这篇关于有没有一种方法可以动态执行字符串在.NET中,类似的eval()的JavaScript或动态SQL的SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆