如何在TextBox中运行动态代码? [英] How to Run the dynamic code in TextBox ?

查看:91
本文介绍了如何在TextBox中运行动态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要知道的东西请帮帮我。



我可以用吗文本框中的代码??

Ex:



我有一个用于编写代码的文本框和另外两个用于输入值的文本。 />


我在第一个文本框中写这些:



Convert.ToDouble(textbox2.Text)+ Convert.ToDouble (textbox3.Text);



当ı点击按钮我希望它被计算出来。并将结果看作是一个laber或其他什么的字符串。



我读了很多文章,但我仍然无法做到。请帮我。谢谢大家。

解决方案

基本上,你可以通过Code DOM来实现。请查看我过去的答案:

使用CodeDom生成代码 [ ^ ],

数学评估用户定义变量 [ ^ ]。



这个问题有一个难点:如果你重复加载通过Code DOM生成的程序集(或者任何程序集,就此而言),除非你在一个单独的<$中加载它们,否则没有办法卸载它们。 c $ c> AppDomain ,您可以卸载如果不卸载,将导致内存泄漏。因此,如果加载的程序集的数量在一开始就不是静态知道的,则必须卸载并处理 AppDomain 实例,但是通过域边界工作更加困难,因为它们的地址空间彼此隔离,所以它意味着使用 IPC 。幸运的是, AppDomain 提供了一些简化的IPC机制。



有关详细信息,请参阅我过去的答案:

一般插件的用途是什么 [ ^ ],

通过它的字符串表示从程序集中收集类型 [ ^ ],

在现有实例上 C#Reflection InvokeMember [ ^ ],

动态加载用户控件 [ ^ ],

创建使用可重新加载插件的WPF应用程序...... [ ^ ] ,

AppDomain拒绝加载程序集 [ ^ ]。



-SA


< blockquote>要执行字符串的内容,就像它是代码一样,你需要一个.NET C#不提供的工具。



这里有很多例子CodeProject [ ^ ]提供此类功能。有些只提供评估简单数学表达式的能力,有些则更复杂,并且允许您在应用程序中引用实例化对象。



CP上的示例使用多种不同的策略,从在C#中实现嵌入式脚本语言到高级JavaScript的'Eval运算符,到其他不那么奇特的方法。



你可以从一开始在CP上查看此解决方案,它可以让您访问对象和字段,如您在示例中所需要的那样:[ ^ ]。



请记住,不过,一旦你允许某人在运行时执行字符串作为代码,你就有可能让某人输入会破坏你的应用程序的代码。


Hi all,

I need something to know please help me.

Can I use the code in textbox??
For Ex:

I have a textbox to write the code and two other to enter values.

I write these in my first textbox :

Convert.ToDouble(textbox2.Text)+ Convert.ToDouble(textbox3.Text);

and when ı click the button I want it to be calculated. and see the result as a string on a laber or whatever.

I read soo much articles but still I cant do it. Please help me. Thanks to all.

解决方案

Basically, you can do it via Code DOM. Please see my past answers:
code generating using CodeDom[^],
mathematic evaluation user defined variable[^].

There is one difficulty in this problem: if you repetitively load assemblies generated via Code DOM (or any assemblies, for this matter), there is no a way to unload them, unless you load each in a separate AppDomain, which you can unload If you don't unload, it will cause memory leak. So, if the number of loaded assemblies is not statically known in the very beginning, you have to unload and deal with AppDomain instances, but working through a domain boundary is more difficult, as their address spaces are isolated from each other, so it means using IPC. Fortunately, AppDomain provides some simplified IPC mechanism.

For further detail, please see my past answers:
What is the use of plugin in general[^],
Gathering types from assemblies by it's string representation[^],
C# Reflection InvokeMember on existing instance[^],
Dynamically Load User Controls[^],
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^].

—SA


To execute the contents of a string, as if it were code, you need a facility that .NET C# does not provide.

There are many examples here on CodeProject [^] that provide this type of functionality. Some provide only the ability to evaluate simple mathematical expressions, others are more complex, and allow you to reference instantiated objects in your application.

The examples on CP use a variety of different strategies, from implementing an embedded scripting language in C# to hi-jacking JavaScript's 'Eval operator, to other, less exotic, methods.

You might start off by looking at this solution on CP which does let you access objects and fields, as you need to do in your example: [^].

Do keep in mind, however, that once you allow someone to execute a string as code at run-time, you have created a possibility for someone to enter code that will screw up your application.


这篇关于如何在TextBox中运行动态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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