我想执行一行存储在字符串中的C#代码 [英] I want to execute a line of C# code stored in a string

查看:69
本文介绍了我想执行一行存储在字符串中的C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提前感谢您的帮助。我想做的是



我有一个字符串

 String cS =" int i = varX + varY" 

我想要完成的是执行int i = ....代码,能够从设置的值设置varX和varY运行。一个基本的想法是



int VarX = 2;


int varY = 3;


Code.Execute(cS)



我将获得5作为其值。我也希望能够在别处使用i的值。

解决方案

参见以下帖子


https://stackoverflow.com/questions/53844/how - 可以-I-评估-AC-尖锐表达-动态


Thanks in advance for the help. What I want to do is

I have a string

String cS = "int i = varX + varY"

What I want to accomplish is execute the int i = .... code, be able to set varX and varY from values set at run time. A basic idea is

int VarX = 2;

int varY = 3;

Code.Execute(cS)

and i will have 5 as its value. I also want to be able to use the value of i elsewhere.

解决方案

See the following thread

https://stackoverflow.com/questions/53844/how-can-i-evaluate-a-c-sharp-expression-dynamically


这篇关于我想执行一行存储在字符串中的C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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