oleg-shilo/cs-script

how to use this to run csharp string as script in netapp

Closed this issue · 4 comments

for example:
code:

 using System;
 using ns = My.other.NameSpce.Foo;// the net app
 
 public class Script
 {
      public int Sum(int a, int b)
      {
          return a+b;
      }
 }
 
 var s = new Script();
 var result = s.Sum(2, 3);
 Console.WriteLine(result);
 
 System.Console.WriteLine(ns.Bar(result));// call the bar functions  of the host net app
 
 System.Console.WriteLine("The code run end");

I want to run the code(string) in my net app by using CSScript.
how can we?

is there any API like CSScript.Evaluator.Run(string codestring) for us to evaluate
thanks

The example above is not using CS-Scriprt but Roslyn API. But I assume your question was about CS-Script (since it is CS-Script project page). 😄 If it is correct then have a look at the corresponding Wiki: https://github.com/oleg-shilo/cs-script/wiki/Hosted-Script-Execution

This is the simplest possible sample from it:

dynamic calc = CSScript.Evaluator
                       .LoadMethod(@"int Multiply(int a, int b)
                                     {
                                         return a * b;
                                     }");

int result = calc.Multiply(3, 2);

If you add CS-Script nuget package to your VS project, it will also add the samples file to it.

no, your example is only the method, I also need to run the method in the script
I want to run this

 using System;
 using ns = My.other.NameSpce.Foo;// the net app
 
 public class Script
 {
      public int Sum(int a, int b)
      {
          return a+b;
      }
 }
 
 var s = new Script();
 var result = s.Sum(2, 3);
 Console.WriteLine(result);
 
 System.Console.WriteLine(ns.Bar(result));// call the bar functions  of the host net app
 
 System.Console.WriteLine("The code run end");

with one API,

Please read the Wiki that I provided the link for. As I indicated I only gave you the simplest example and the article contains plenty of others.

And if you do use the nuget package I suggested it will bring all other possible samples.

You can also browse some of the samples in the repo:
image