C# Class Mono.CSharp.Evaluator

Evaluator: provides an API to evaluate C# statements and expressions dynamically.
This class exposes static methods to evaluate expressions in the current program. To initialize the evaluator with a number of compiler options call the Init(string[]args) method with a set of command line options that the compiler recognizes. To interrupt execution of a statement, you can invoke the Evaluator.Interrupt method.
显示文件 Open project: praeclarum/runcs Class Usage Examples

Public Properties

Property Type Description
DescribeTypeExpressions bool

Public Methods

Method Description
Compile ( string input ) : CompiledMethod

Compiles the input string and returns a delegate that represents the compiled code.

Compiles the input string as a C# expression or statement, unlike the Evaluate method, the resulting delegate can be invoked multiple times without incurring in the compilation overhead. This method can only deal with fully formed input strings and does not provide a completion mechanism. If you must deal with partial input (for example for interactive use) use the other overload. On success, a delegate is returned that can be used to invoke the method.

Compile ( string input, CompiledMethod &compiled ) : string

Compiles the input string and returns a delegate that represents the compiled code.

Compiles the input string as a C# expression or statement, unlike the Evaluate method, the resulting delegate can be invoked multiple times without incurring in the compilation overhead. If the return value of this function is null, this indicates that the parsing was complete. If the return value is a string it indicates that the input string was partial and that the invoking code should provide more code before the code can be successfully compiled. If you know that you will always get full expressions or statements and do not care about partial input, you can use the other Compile overload. On success, in addition to returning null, the compiled parameter will be set to the delegate that can be invoked to execute the code.

Evaluate ( string input ) : object

Evaluates and expression or statement and returns the result.

Evaluates the input string as a C# expression or statement and returns the value. This method will throw an exception if there is a syntax error, of if the provided input is not an expression but a statement.

Evaluate ( string input, object &result, bool &result_set ) : string

Evaluates and expression or statement and returns any result values.

Evaluates the input string as a C# expression or statement. If the input string is an expression the result will be stored in the result variable and the result_set variable will be set to true. It is necessary to use the result/result_set pair to identify when a result was set (for example, execution of user-provided input can be an expression, a statement or others, and result_set would only be set if the input was an expression. If the return value of this function is null, this indicates that the parsing was complete. If the return value is a string, it indicates that the input is partial and that the user should provide an updated string.

Evaluator ( CompilerSettings settings, Report report ) : System
GetCompletions ( string input, string &prefix ) : string[]
GetUsing ( ) : string
GetVars ( ) : string
Init ( ) : void
Interrupt ( ) : void

Interrupts the evaluation of an expression executing in Evaluate.

Use this method to interrupt long-running invocations.

LoadAssembly ( string file ) : void

Loads the given assembly and exposes the API to the user.

ReferenceAssembly ( Assembly a ) : void

Exposes the API of the given assembly to the Evaluator

Run ( string statement ) : bool

Executes the given expression or statement.

Executes the provided statement, returns true on success, false on parsing errors. Exceptions might be thrown by the called code.

Private Methods

Method Description
CompileBlock ( Class host, Undo undo, Report Report ) : CompiledMethod
GetUsingList ( ) : ICollection
GetVarNames ( ) : string[]
LookupField ( string name ) : FieldInfo>.Tuple
ParseString ( ParseMode mode, string input, bool &partial_input ) : CSharpParser
Quote ( string s ) : string
Reset ( ) : void
ToplevelOrStatement ( Mono.CSharp.SeekableStreamReader seekable ) : InputKind

Method Details

Compile() public method

Compiles the input string and returns a delegate that represents the compiled code.
Compiles the input string as a C# expression or statement, unlike the Evaluate method, the resulting delegate can be invoked multiple times without incurring in the compilation overhead. This method can only deal with fully formed input strings and does not provide a completion mechanism. If you must deal with partial input (for example for interactive use) use the other overload. On success, a delegate is returned that can be used to invoke the method.
public Compile ( string input ) : CompiledMethod
input string
return CompiledMethod

Compile() public method

Compiles the input string and returns a delegate that represents the compiled code.
Compiles the input string as a C# expression or statement, unlike the Evaluate method, the resulting delegate can be invoked multiple times without incurring in the compilation overhead. If the return value of this function is null, this indicates that the parsing was complete. If the return value is a string it indicates that the input string was partial and that the invoking code should provide more code before the code can be successfully compiled. If you know that you will always get full expressions or statements and do not care about partial input, you can use the other Compile overload. On success, in addition to returning null, the compiled parameter will be set to the delegate that can be invoked to execute the code.
public Compile ( string input, CompiledMethod &compiled ) : string
input string
compiled CompiledMethod
return string

Evaluate() public method

Evaluates and expression or statement and returns the result.
Evaluates the input string as a C# expression or statement and returns the value. This method will throw an exception if there is a syntax error, of if the provided input is not an expression but a statement.
public Evaluate ( string input ) : object
input string
return object

Evaluate() public method

Evaluates and expression or statement and returns any result values.
Evaluates the input string as a C# expression or statement. If the input string is an expression the result will be stored in the result variable and the result_set variable will be set to true. It is necessary to use the result/result_set pair to identify when a result was set (for example, execution of user-provided input can be an expression, a statement or others, and result_set would only be set if the input was an expression. If the return value of this function is null, this indicates that the parsing was complete. If the return value is a string, it indicates that the input is partial and that the user should provide an updated string.
public Evaluate ( string input, object &result, bool &result_set ) : string
input string
result object
result_set bool
return string

Evaluator() public method

public Evaluator ( CompilerSettings settings, Report report ) : System
settings CompilerSettings
report Report
return System

GetCompletions() public method

public GetCompletions ( string input, string &prefix ) : string[]
input string
prefix string
return string[]

GetUsing() public method

public GetUsing ( ) : string
return string

GetVars() public method

public GetVars ( ) : string
return string

Init() public method

public Init ( ) : void
return void

Interrupt() public method

Interrupts the evaluation of an expression executing in Evaluate.
Use this method to interrupt long-running invocations.
public Interrupt ( ) : void
return void

LoadAssembly() public method

Loads the given assembly and exposes the API to the user.
public LoadAssembly ( string file ) : void
file string
return void

ReferenceAssembly() public method

Exposes the API of the given assembly to the Evaluator
public ReferenceAssembly ( Assembly a ) : void
a System.Reflection.Assembly
return void

Run() public method

Executes the given expression or statement.
Executes the provided statement, returns true on success, false on parsing errors. Exceptions might be thrown by the called code.
public Run ( string statement ) : bool
statement string
return bool

Property Details

DescribeTypeExpressions public_oe property

If true, turns type expressions into valid expressions and calls the describe method on it
public bool DescribeTypeExpressions
return bool