在我的表单上,我点击了一个按钮
private void button1_Click(object sender, EventArgs e)
{
do something
}
单击时如何从文本文件加载我的操作,例如我的文本文件如下所示:
MessageBox.Show("hello");
label1.Text = "Hello";
如果可能的话,单击它会执行我的文本文件中的所有操作。
最佳答案
这是一个非常简单的例子,只是为了证明这是可能的。基本上,您使用 CodeDomProvider
在运行时编译源代码,然后使用反射执行。
var provider = CodeDomProvider.CreateProvider("C#");
string src=@"
namespace x
{
using System;
public class y
{
public void z()
{
Console.WriteLine(""hello world"");
}
}
}
";
var result = provider.CompileAssemblyFromSource(new CompilerParameters(), src);
if (result.Errors.Count == 0)
{
var type = result.CompiledAssembly.GetType("x.y");
var instance = Activator.CreateInstance(type);
type.GetMethod("z").Invoke(instance, null);
}
编辑
正如@Agat指出的,OP似乎需要一种脚本框架(它使用label1
,当前对象的属性),而我上面的答案显然没有提供这一点。我能想到的最好的解决方案是一个有限的解决方案,这将要求将依赖项明确指定为“脚本”中的参数。例如,编写如下脚本代码:
string src = @"
namespace x
{
using System.Windows;
public class y
{
public void z(Label label1)
{
MessageBox.Show(""hello"");
label1.Text = ""Hello"";
}
}
}
";
现在您可以让调用者检查参数,并再次使用反射从当前上下文传递它们:
var result = provider.CompileAssemblyFromSource(new CompilerParameters(), src);
if (result.Errors.Count == 0)
{
var type = result.CompiledAssembly.GetType("x.y");
var instance = Activator.CreateInstance(type);
var method = type.GetMethod("z");
var args = new List<object>();
// assume any parameters are properties/fields of the current object
foreach (var p in method.GetParameters())
{
var prop = this.GetType().GetProperty(p.Name);
var field = this.GetType().GetField(p.Name);
if (prop != null)
args.Add(prop.GetValue(this, null));
else if (field != null);
args.Add(field.GetValue(this));
else
throw new InvalidOperationException("Parameter " + p.Name + " is not found");
}
method.Invoke(instance, args.ToArray());
}
关于c# - 是否可以执行以字符串表示的 C# 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21445147/