从 txt 文件编译 C# 代码以与正在运行的 wpf 应用程序交互

发布于 2024-10-30 21:22:15 字数 337 浏览 0 评论 0原文

我一直在网上寻找一种在运行时编译代码并操作正在运行的应用程序的对象(属性等)的简洁方法。我遇到过 Snippy、CodeDom 和 CSharpCodeProvider,但我并不完全理解如何在我的应用程序中使用这些解决方案来完成我想要的事情。 最重要的是,我希望在外部文件中包含一小部分代码,以便我可以在运行时交换不同的代码(例如操作数据的公式等)有人可以向我解释一下我到底如何在一个简洁时尚的 WPF 应用程序?我只需要向外部代码传递一些数据,执行后它将返回一些我可以用来填充对象的数据。 PS:我还考虑过从字符串解析数学表达式并以这种方式操作我的数据,但如果我可以在运行时从外部解析和执行 C# 代码,它将给我更多的自由和对公式和数据的控制。提前致谢。

I have been searching online for a neat way to compile code at runtime and manipulate the running application's objects (properties etc.). I have come across Snippy, CodeDom and CSharpCodeProvider but I didn't completely understood how I can use those solutions in my application to do what I want.
Bottom line is, I want to have a small portion of the code in an external file so I can swap out different code during run time (e.g. Formulas to manipulate Data and etc.) Could somebody explain to me how exactly can I implement this in a WPF application in a neat fashion? I just need to pass the external code some data and after execution it will return me some data that I can populate an object with.
P.S: I also thought about parsing Mathematical expression from String and manipulate my data that way but if I can parse and execute C# code externally at run time, it will give me much more freedom and control over the formula and data. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千紇 2024-11-06 21:22:15

我认为你会更好地使用 .NET 兼容的动态语言,如 IronPythonIronRuby。它们与 C#/.NET 几乎无缝集成,实际上旨在提供执行时脚本能力。

Jon Skeet 的 C# 深入了解中的 IronPython 使用示例:

// the python code
string python = @" 
text = 'hello' 
output = input + 1 
";

// setup the scripting engine
ScriptEngine engine = Python.CreateEngine(); 
ScriptScope scope = engine.CreateScope(); 
scope.SetVariable("input", 10); 
engine.Execute(python, scope); 

// the results
Console.WriteLine(scope.GetVariable("text")); 
Console.WriteLine(scope.GetVariable("input")); 
Console.WriteLine(scope.GetVariable("output"));

不用说,脚本可以使用 .NET 基类库以及您向它们公开的任何额外方法。

I think you would do better using .NET compatible dynamic language like IronPython or IronRuby. Those integrate pretty much seamlessly with C#/.NET and are actually designed to provide execution-time scriptability.

Trivial example of IronPython usage from Jon Skeet's C# in Depth:

// the python code
string python = @" 
text = 'hello' 
output = input + 1 
";

// setup the scripting engine
ScriptEngine engine = Python.CreateEngine(); 
ScriptScope scope = engine.CreateScope(); 
scope.SetVariable("input", 10); 
engine.Execute(python, scope); 

// the results
Console.WriteLine(scope.GetVariable("text")); 
Console.WriteLine(scope.GetVariable("input")); 
Console.WriteLine(scope.GetVariable("output"));

Needless to say, the scripts can use .NET Base Class Library and any extra methods you expose to them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文