Is there a way to dynamically execute a string in .net, similar to eval() in javascript or dynamic sql in sql?

前端 未结 8 1459
心在旅途
心在旅途 2020-12-05 12:39

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?

I have a st

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 13:08

    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.

    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.

    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.

    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
    

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

提交回复
热议问题