在文本框中执行代码 [英] execute codes in textbox

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

问题描述

你好.
如何制作执行用户键入的代码的程序?
例如在我的表单中有一个文本框,当用户在其中键入c#代码并按执行按钮时,我的程序将文本框的文本作为c#代码执行.

谢谢所有亲爱的朋友.

Hello there.
how can i make a program that execute codes that typed by user?
for example in my form there is a textbox and when user type a c# code in it and press execute button, my program execute textbox''s text as c# code.

thank you all dear friends.

推荐答案

您可以使用Roslyn. 将Roslyn ScriptEngine用于ValueConverter处理用户输入 [ ^ ]
You can use Roslyn. A Codeproject article covering using Roslyn for execution is coverer in Using Roslyn ScriptEngine for a ValueConverter to process user input[^]


当有人告诉您某物的名称时,这意味着您可以将其键入google并寻求帮助.如果您知道它的名字,却无法在Google上搜索,那么您并不是在真正地学习如何成为一名程序员.

此处 [
When someone tells you the name of something, it means you can type it in to google and find help. If you know what it''s called and you can''t google it, then you''re not really learning how to be a programmer.

Here[^], I did it for you.


这不像Cristian那样容易告诉过你了.它会一次又一次地工作,但是问题是,如果您一次又一次地将重新构建的程序集加载到宿主应用程序过程中,那么通常需要多次重复用户键入的代码的编译.它将导致内存泄漏,这可能会很大.原因如下:有一种方法可以加载另一个程序集,但没有没有方法可以卸载它.这样设计是因为卸载是不安全的:什么代码将调用不再存在的方法?除一种情况外:您可以将其加载到单独的应用程序域中,然后卸载整个域.

因此,每次用户想要执行新的编辑代码,构建和加载程序集,调用某些方法然后卸载应用程序域时,您都需要创建一个新的应用程序域.但是随后,您将需要遍历应用程序域边界,因为域是隔离的以及独立的进程.这种分离意味着使用IPC,这很麻烦.尽管如此,这还是有可能实现的.

请查看我过去对相关问题的回答:
创建使用可重载插件的WPF应用程序... [^ ],
AppDomain拒绝加载程序集 [使用CodeDom生成代码 [创建使用可重载插件的WPF应用程序... [ ^ ];

当您不需要重新加载程序集时,这就是它的简单部分:
动态加载用户控件 [在现有实例上的C#反射InvokeMember [
This is not so easy as Cristian told you. It will work once and more, but the problem is when you need to repeat the compilation of the code typed by the user multiple times if you load the freshly built assembly in your host application process again and again, as it is usually required. It will create a memory leak, which could be big. Here is why: there is a way to load yet another assembly, but there is no a way to unload it. This is designed so because unloading is unsafe: what is some code will call a method which does not exist anymore? Except one case: you can load it in a separate application domain and unload the whole domain.

So, you would need to create a new application domain each time the user wants to execute a new edited code, build and load assembly, call some method and then unload the application domain. But then you will need to work through the application domain boundary, as domain are isolated as well as separate processes. Such separation means using IPC, which is a hassle. Nevertheless, this is quite possible to implement.

Please see my past answers to related questions:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^];

As related to DOM and code parsing:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^];

This is about the simple part of it, when you don''t need to reload assemblies:
Dynamically Load User Controls[^],
C# Reflection InvokeMember on existing instance[^].

—SA


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

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