CSharpScriptExecution Class
Class that can be used to execute code snippets or entire blocks of methods dynamically. Two methods are provided:
- ExecuteCode - executes code. Pass parameters and return a value * ExecuteMethod - lets you provide one or more method bodies to execute * Evaluate - Evaluates an expression on the fly (uses ExecuteCode internally) * CompileClass - compiles a class and returns the a class instance
Assemblies used for execution are cached and are reused for a given block of code provided.
Westwind.Scripting.CSharpScriptExecution
Class Members
| Member | Description | |
|---|---|---|
| Constructor | This site provides class reference documentation for the classes of the Westwind.Scripting library. Class Reference Highlights CSharpScriptExecution Class - C# Code and Expressions… | |
| AddAssemblies | Adds a list of assemblies to the References collection.
public void AddAssemblies(IEnumerable
|
|
| AddAssembly | Adds an assembly reference from an existing type
public bool AddAssembly(string assemblyDll)
|
|
| AddDefaultReferencesAndNamespaces | Adds core system assemblies and namespaces for basic operation. Any additional references need to be explicitly added. Alternatelively use: AddLoadedReferences()
public void AddDefaultReferencesAndNamespaces()
|
|
| AddLoadedReferences | Explicitly adds all referenced assemblies of the currently executing process. Also adds default namespaces. Useful in .NET Core to ensure that all those little tiny system assemblies that comprise…
public void AddLoadedReferences()
|
|
| AddNamespace | Adds a namespace to the referenced namespaces used at compile time.
public void AddNamespace(string nameSpace)
|
|
| AddNamespaces | Adds a set of namespace to the referenced namespaces used at compile time.
public void AddNamespaces(String[] namespaces)
|
|
| AddNetCoreDefaultReferences | This site provides class reference documentation for the classes of the Westwind.Scripting library. Class Reference Highlights CSharpScriptExecution Class - C# Code and Expressions…
public void AddNetCoreDefaultReferences()
|
|
| AddNetFrameworkDefaultReferences |
public void AddNetFrameworkDefaultReferences()
|
|
| CompileAssembly | Compiles the source code for a complete class and then loads the resulting assembly. Loads the generated assembly and sets the .Assembly property if successful. If OutputAssembly is set, the assembly…
public bool CompileAssembly(string source, bool noLoad)
|
|
| CompileClass | This method compiles a class and hands back a dynamic reference to that class that you can call members on. Must have include parameterless ctor()
public object CompileClass(string code)
|
|
| CompileClassToType | This method expects a fully self-contained class file including namespace and using wrapper to compile from an input stream.
public Type CompileClassToType(string code)
|
|
| CreateDefault | Creates a default Execution Engine which has: AddDefaultReferences and Namespaces set SaveGeneratedCode = true Optionally allows to pass in references and namespaces
public CSharpScriptExecution CreateDefault(String[] references, String[] namespaces,
Type[] referenceTypes)
|
|
| CreateInstance | Creates an instance of the object specified by the GeneratedNamespace and GeneratedClassName in the currently active, compiled assembly Sets the ObjectInstance member which is returned and which can…
public object CreateInstance(bool force)
|
|
| Evaluate | Evaluates a single value or expression that returns a value.
public object Evaluate(string code, object[] parameters)
|
|
| EvaluateAsync | Evaluates an awaitable expression that returns a value Example: script.EvaluateAsync("await ActiveEditor.GetSelection()",model);
public Task
|
|
| ExecuteCode | Executes a snippet of code. Pass in a variable number of parameters (accessible via the parameters[0..n] array) and return an object parameter. Code should include: return (object) SomeValue as the…
public object ExecuteCode(string code, object[] parameters)
|
|
| ExecuteCodeAsync | Executes a snippet of code. Pass in a variable number of parameters (accessible via the parameters[0..n] array) and return an object value. Code should always return a result: include: return…
public Task
|
|
| ExecuteCodeFromAssembly | Executes a method from an assembly that was previously compiled
public object ExecuteCodeFromAssembly(string code, Assembly assembly,
object[] parameters)
|
|
| ExecuteCodeFromAssemblyAsync |
public Task
|
|
| ExecuteMethod | Executes a complete method by wrapping it into a class, compiling and instantiating the class and calling the method. Code should include full method header (instance type, return value and…
public object ExecuteMethod(string code, string methodName,
object[] parameters)
|
|
| ExecuteMethodAsync | Executes a complete async method by wrapping it into a class, compiling and instantiating the class and calling the method. This method has to return a result value - it cannot be void! Class should…
public Task
|
|
| ExecuteMethodAsyncVoid | Executes a complete async method by wrapping it into a class, compiling and instantiating the class and calling the method. This method returns no value. Class should include full class header…
public Task ExecuteMethodAsyncVoid(string code, string methodName,
object[] parameters)
|
|
| ExecuteScript |
public string ExecuteScript
|
|
| ExecuteScriptAsync | Internal reference to the Assembly Generated
public Task
|
|
| FindCodeLine | Returns 0 offset line number where matched line lives
public int FindCodeLine(string code, string matchLine)
|
|
| GetLines | Parses a string into an array of lines broken by \r\n or \n
public String[] GetLines(string s, int maxLines)
|
|
| InvokeMethod | Helper method to invoke a method on an object using Reflection
public object InvokeMethod(object instance, string method,
object[] parameters)
|
|
| ToString |
public string ToString()
|
|
| AllowReferencesInCode | If true parses references in code that are referenced with: #r assembly.dll | |
| AlternateAssemblyLoadContext | The AssemblyLoadContext the assembly should be loaded in. If not assigned, assemblies will get loaded by the default Assembly.Load methods If the alternate AssemblyLoadContext is assigned, that will… | |
| Assembly | Internal reference to the Assembly Generated | |
| CodeInjection | Optional code injections at various points in the code generation | |
| CompileWithDebug | Determines whether the code is compiled in Debug or Release mode Defaults to Release and there's no good reason for scripts to use anything else since debug info is not available in Reflection… | |
| DisableAssemblyCaching | If disabled, assemblies will not be cached through hashes Instead for each execution a new unique assembly will get generated and loaded This combined with an alternate AssemblyLoadContext can lower… | |
| DisableObjectCaching | Disables caching object instances that are created after CompileClass If true you can't execute new code on the instance as the class instance is cached. If false the instance is cached and… | |
| Error | Error flag that is set if an error occurred during the invoked method or script call | |
| ErrorMessage | Error message if an error occurred during the invoked method or script call | |
| ErrorType | Determines whether the error is a compile time error or runtime error | |
| GeneratedClassCode | Last generated code for this code snippet if SaveGeneratedCode = true | |
| GeneratedClassCodeWithLineNumbers | Last generated code for this code snippet with line numbers | |
| GeneratedClassName | Name of the class when a class name is not explicitly provided as part of the code compiled. Always used for the module name. By default this value is a unique generated id. Make sure if you create… | |
| GeneratedNamespace | Name of the namespace that a class is generated in | |
| LastException | Last Exception fired when a runtime error occurs Generally this only contains the error message, but there's no call stack information available due to the Reflection or dynamic code invocation | |
| Namespaces | List of additional namespaces to add to the script | |
| ObjectInstance | Internal reference to the generated type that is to be invoked Can also be used to cache an object instance using ReuseObjectInstance, which is useful if you work with a single object/method that is… | |
| OutputAssembly | Filename for the output assembly to generate. If empty the assembly is generated in memory (dynamic filename managed by the .NET runtime) | |
| References | List of additional assembly references that are added to the compiler parameters in order to execute the script code. | |
| ReuseObjectInstance | If true reuses the ObjectInstance fromt he previous operation (if set) instead of creating a new instance. Use if you pre-generate a single instance and call one or more methods/scripts repeatedly… | |
| SaveGeneratedCode | Determines whether GeneratedCode will be set with the source code for the full generated class | |
| ThrowExceptions | If true throws exceptions when executing the code rather than setting the Error, ErrorMessage and LastException properties. Note: Compilation errors will not throw, but always set properties! | |
Assembly: Westwind.Scripting.dll
