scripting support in a C# application [closed]

what is the simplest and fastest way to support simple scripting in a .Net application?
I search a lot but only find many things with practical no documentation or outdated since years.
I only need to transfer a simple .Net Object to the script and get a bool result from it.

5

It depends on your precise needs. Two ways I can think of:

  • Use a language like Python which is interoperable with C#, and call the Python script from C#. Actually, you can even make things simpler by just calling an executable, passing to it an XML or JSON data, and wait for XML or JSON response.

  • If you need to use C#, then compile C# on the fly, just like LINQPad does. Is it possible to dynamically compile and execute C# code fragments? is a good starting point. Loading the code in a sandbox (in a different AppDomain) is also a good idea if you need to provide enough security and don’t trust the code which can be written by the user.

0

I think the simplest way would be to use one of the .Net scripting languages that already exist, like IronPython or IronRuby.

For example, with IronPython you can write your script code in Python, execute it from your C# application and then process the results from the script.

The code could look like this:

var engine = Python.CreateEngine();
var scope = engine.CreateScope();
scope.SetVariable("value", 43);

var code = @"
# overcomplicated just to show it's really Python
def areEqual (value1, value2):
    return value1 == value2

areEqual(value, 42)";

Console.WriteLine(engine.Execute<bool>(code, scope));

Another option would be to use the scripting capabilities of Roslyn, but that’s not production ready yet.

Another scripting language you should consider is PowerShell. There’s a fully working example of an application that exposes some of its internal objects to a script environment here.

This is the description of the application from the book (emphasis mine):

The reference application for this chapter, Beaver Music, is a very simple music album management system. It supports create, read, update, and delete (CRUD) actions for albums. Beaver Music has the functionality you’d expect—a couple of dialogs for adding and changing album information, and you can delete albums as well. What we want to focus on is the PowerShell Console button (shown in Figure 5-1), a WPF application that has the PowerShell engine embedded in it. As noted earlier, PowerShell is surfaced as a console, a scripting language, and an API; the custom PowerShell console uses this surfaced API in conjunction with the Beaver Music application so it can be scripted and automated. This works similarly to the way Microsoft Excel can be automated with the embedded Visual Basic for Applications (VBA) scripting language.

You could build a temp class and invoke a method on that

var template = @"public static class Temp
{{
  public static bool Run()
  {{
      return {0};
  }}
}}";

var script = "1 < 2";

var source = string.Format(template, script);

var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } });
var parameters = new CompilerParameters(new[] { "mscorlib.dll" }, "temp.dll", true);
parameters.GenerateExecutable = false;
var results = csc.CompileAssemblyFromSource(parameters, source);
var type = results.CompiledAssembly.GetType("Temp");
var method = type.GetMethod("Run");
var result = (bool)method.Invoke(null, null);
Console.WriteLine(result);

But you really have to trust people who write the scripts. Just add parameters to the Run method to pass your object.

Fastest and simplest? I really hope this isn’t that, but as a really last resort, you can use the .NET Emit method to create hand crafted CIL code that you generate dynamically in your program.

If your scripting requirement is really trivial, then this is probably a month or two’s work. You can work out the CIL instruction set from using a reflector to look at how the compiler translates your C# code into CIL, and then ‘borrow’ these code fragments.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật