从 txt 文件编译 C# 代码以与正在运行的 wpf 应用程序交互
我一直在网上寻找一种在运行时编译代码并操作正在运行的应用程序的对象(属性等)的简洁方法。我遇到过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你会更好地使用 .NET 兼容的动态语言,如 IronPython 或 IronRuby。它们与 C#/.NET 几乎无缝集成,实际上旨在提供执行时脚本能力。
Jon Skeet 的 C# 深入了解中的 IronPython 使用示例:
不用说,脚本可以使用 .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:
Needless to say, the scripts can use .NET Base Class Library and any extra methods you expose to them.