New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to execute a String as code during run-time? #30

Closed
DinisCruz opened this Issue Aug 23, 2014 · 1 comment

Comments

Projects
None yet
1 participant
@DinisCruz
Contributor

DinisCruz commented Aug 23, 2014

"I want to be able to execute a String as code during run-time and it
should be able to reference other namespaces, application objects
(chart nodes) and load classes saved in .cs files."

(from O2 User)

@DinisCruz

This comment has been minimized.

Show comment
Hide comment
@DinisCruz

DinisCruz Aug 23, 2014

Contributor

To execute CSharp code (from code snippets) you will need the FluentSharp.REPL assembly (which you can get from Nuget here) and use it's extension methods to compile and execute the code:

  1. simple string execution
return "return 2+2;".compileAndExecuteCodeSnippet();

image

  1. using a variable to hold the code
var code = @"

var value = 40 + +2;
return value;
";
return code.compileAndExecuteCodeSnippet();

image

  1. compiling and executing in separate steps:
var code = @"

var value = 40 + +2;
return value;
";
var assembly = code.compile_H2Script();
return assembly.executeFirstMethod();

image

note that compilation errors will be shown on the Log viewer:

image

or

image

  1. Loading an external DLL and assessing it via reflection
var code = @"
return ""TeamMentor.Schemas.dll"".assembly().types();

//O2Ref:E:\TeamMentor\TM_Dev Repos\TM_Dev\TeamMentor\Publish\Website.3.5\bin\TeamMentor.Schemas.dll
";
var assembly = code.compile_H2Script();
return assembly.executeFirstMethod();

image

  1. Directly using an type loaded from an external DLL
var code = @"
var article = new TeamMentor_Article();
article.Metadata.Title = ""Custom Article"";
return article.Metadata;

//using TeamMentor.CoreLib;
//O2Ref:E:\TeamMentor\TM_Dev Repos\TM_Dev\TeamMentor\Publish\Website.3.5\bin\TeamMentor.Schemas.dll
";
var assembly = code.compile_H2Script();
return assembly.executeFirstMethod();

image

Contributor

DinisCruz commented Aug 23, 2014

To execute CSharp code (from code snippets) you will need the FluentSharp.REPL assembly (which you can get from Nuget here) and use it's extension methods to compile and execute the code:

  1. simple string execution
return "return 2+2;".compileAndExecuteCodeSnippet();

image

  1. using a variable to hold the code
var code = @"

var value = 40 + +2;
return value;
";
return code.compileAndExecuteCodeSnippet();

image

  1. compiling and executing in separate steps:
var code = @"

var value = 40 + +2;
return value;
";
var assembly = code.compile_H2Script();
return assembly.executeFirstMethod();

image

note that compilation errors will be shown on the Log viewer:

image

or

image

  1. Loading an external DLL and assessing it via reflection
var code = @"
return ""TeamMentor.Schemas.dll"".assembly().types();

//O2Ref:E:\TeamMentor\TM_Dev Repos\TM_Dev\TeamMentor\Publish\Website.3.5\bin\TeamMentor.Schemas.dll
";
var assembly = code.compile_H2Script();
return assembly.executeFirstMethod();

image

  1. Directly using an type loaded from an external DLL
var code = @"
var article = new TeamMentor_Article();
article.Metadata.Title = ""Custom Article"";
return article.Metadata;

//using TeamMentor.CoreLib;
//O2Ref:E:\TeamMentor\TM_Dev Repos\TM_Dev\TeamMentor\Publish\Website.3.5\bin\TeamMentor.Schemas.dll
";
var assembly = code.compile_H2Script();
return assembly.executeFirstMethod();

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment