Questions tagged [csharpscript]

CSharpScript is a CLR based scripting system which uses the C# programming language.

CSharpScript or CS-Script is a CLR (Common Language Runtime) based scripting system (Script execution engine) which uses ECMA-compliant C# as a programming language. CSharpScript currently targets Microsoft implementation of CLR (.NET 2.0/3.0/3.5/4.0/4.5) with full support on Mono.

Hello.cs

using System;
using System.Windows.Forms;
class Script {
    static void Main() {
        MessageBox.Show("Hello World!");
    }
}

Hosting script engine

dynamic script = CSScript.Evaluator
                         .LoadMethod(@"void SayHello(string greeting) {
                                   Console.WriteLine(greeting);
                               }");
script.SayHello("Hello World!");

Benefits of C# Scripting System:

  • Simple deployment approach
  • Portability
  • Base language is a full featured C# and also supports VB.NET, C++/CLI and J#
  • Easy conversion of any script into application and vice versa
  • Script language is type safe
  • Extensibility
  • Script hosting
  • brings you Dynamic Code Generation

Useful links:

32 questions
6
votes
1 answer

CSharpScript Using Externally Defined Types - Can't Convert From Type A to A

Problem: Can't use externally defined types in CSharpScript because it can't convert the object type from itself to itself due to some Assembly mismatch I guess. I have 2 projects. Common using System; namespace Common { public class Arguments …
Daniel Wardin
  • 1,840
  • 26
  • 48
4
votes
0 answers

How to resolve dependency issues with C# script csx file in visual studio code?

I am trying to create an azure function program with a service bus queue trigger template using a csx file. But I am having issues resolving dependencies. To be honest, i am very confused about the project structure that is mentioned in [this doc]…
4
votes
0 answers

Managing security and resource usage of C# Scripts

My client wants to add the ability for users to define small scripts that can be used to run various calculations in a pre-existing web application. Currently, we're exploring using CSharpScripts in Microsoft.CodeAnalysis.Scripting.CSharp to…
4
votes
1 answer

How to cancel CSharpScript.RunAsync

How can I stop RunAsync? CancellatioTokenSource cts = new CancellationTokenSource(); //I thought that it's must work, but it don't var script = CSharpScript.Create(code: someCode); await script.RunAsync(cancelletionToken: cts.Token); void…
itihonov
  • 81
  • 1
  • 7
3
votes
1 answer

Definition for 'Select' not working in CSharpScript.EvaluateAsync

I am having problems getting this code to work. It seems that the CSharpScript.EvaluateAsync will not understand the Linq 'Select' command even though I think I am adding the proper references to the ScriptOptions. The goal here is to use the…
ChiliYago
  • 11,341
  • 23
  • 79
  • 126
3
votes
1 answer

System.IO.FileNotFoundException when using CSharpScript in Blazor wasm

I'm trying to use CSharpScript in a Blazor wasm application, testing with a simple EvaluateAsync: var result = await CSharpScript.EvaluateAsync("1 + 1"); Throws: System.IO.FileNotFoundException: Could not find file "/mscorlib.dll" I'm using…
2
votes
1 answer

FileLoadException when running C# script with .NET Core 3.1

I wrote the following C# script (HelloWorld.csx file): #! "netcoreapp3.1" #r "nuget: System.Text.Encoding.CodePages, 5.0.0" public class Script { public static void Run() { try { …
Francois C
  • 1,274
  • 2
  • 12
  • 14
2
votes
2 answers

Method 'CommonCreateArrayTypeSymbol' does not have an implementation

I have very simple code, which uses nuget Microsoft.CodeAnalysis.CSharp.Scripting: var script = CSharpScript.Create($@" IntProp1 = 123; IntProp2 = 456; return IntProp1 + IntProp2; "); var errors = script.Compile(); It was working perfectly fine…
Philipp Munin
  • 5,610
  • 7
  • 37
  • 60
2
votes
1 answer

How to Evaluate Func with use EF.Functions?

I'm trying to evaluate func with EF.Functions.Like in CSharpScript.EvaluateAsync, But I have an error in run time (The name 'EF' does not exist in the current context) when call GetPredicate() method. Create Func Method: public static class…
1
vote
1 answer

How Do I Transplant and Execute User Code in a C# Source Generator?

I am creating a Roslyn .NET source generator. With it I am inspecting user code for lambda expressions given as an argument in a method call like this: // User Code MyLibrary.MyMethod(k => $"alfa/{k.SomeMethod()}"); I want to, within my…
1
vote
0 answers

How do I pass a CSharpScript variable a value or object instance before I run it?

Using CSharpScript, is there a way I can pass a variable a value or object instance before the script is run? it seems like using StateScript will allow me to set a script variable using the following. ScriptState state; state.GetVariable("a").Value…
1
vote
0 answers

CSharpScript. DynamicClass as globals getting CS0234 error

I'm trying to build flexible script evaluator, which сould receive dynamic list of global variables. I'm using DynamicClass from DynamicLinq lib for build global object. But I've got CS0234 error: The type or namespace name 'Dynamic' does not exist…
shura_gaz
  • 11
  • 2
1
vote
1 answer

Dynamically Execute C# with Microsoft.CodeAnalysis.CSharp.Scripting is failing

I have a requirement of executing a C# class in the string format and populate an object with the properties of that class. To achieve the requirement I was doing a POC and that's failed. Following code is failing to evaluate where i was trying to…
sambeet das
  • 53
  • 1
  • 7
1
vote
0 answers

Is there any way to create a conditional reference in a C# script (CSX)?

I have a CSX script that needs to reference a DLL, but the location of the DLL varies; at design time it's in one folder but at runtime it's in another. I tried adding #r directives for both locations but then when I run the app which is calling the…
ekolis
  • 6,270
  • 12
  • 50
  • 101
1
vote
1 answer

Roslyn CSharpScript and DateTime Reference error

error CS0246: The type or namespace name 'DateTime' could not be found (are you missing a using directive or an assembly reference?) When running the following code: public string DateCalculationString { get =>…
JasonDWilson
  • 503
  • 4
  • 10
1
2 3