本文整理汇总了C#中Evaluator.Compile方法的典型用法代码示例。如果您正苦于以下问题:C# Evaluator.Compile方法的具体用法?C# Evaluator.Compile怎么用?C# Evaluator.Compile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Evaluator
的用法示例。
在下文中一共展示了Evaluator.Compile方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateType
public Type CreateType(string code, IEnumerable<Assembly> references, bool emitDebugInfo)
{
code = CodeParser.RandomNamespaceGenerate(code);
var evaluator = new Evaluator(
new CompilerContext(
new CompilerSettings
{
GenerateDebugInfo = emitDebugInfo,
Optimize = emitDebugInfo,
LoadDefaultReferences = !Host.IsWebApp()
},
_printer));
if (references == null)
{
references = AppDomain.CurrentDomain.GetAssemblies()
.Where(
x => x.GetName()
.Name != "mscorlib");
}
references.ToList().ForEach(evaluator.ReferenceAssembly);
evaluator.Compile(code);
var assemblies = this.GetDynamicAssemblies();
Type result = null;
if (assemblies.Any())
{
result = assemblies.LastOrDefault().GetTypes().FirstOrDefault();
}
else
{
var evaluate = string.Format(
"typeof({0}.{1});",
Regex.Match(code, Constants.Regex.Namespace).Value.Trim(),
Regex.Match(code, Constants.Regex.ClassName).Value.Trim());
result = (Type)evaluator.Evaluate(evaluate);
}
return result;
}
示例2: Eval
public void Eval(string code, string usings, IEnumerable<Assembly> references)
{
//code = CodeParser.RandomNamespaceGenerate(code);
var sb = new StringBuilder();
const string SystemUsing = "using System;";
sb.Append(SystemUsing);
sb.Append(usings);
var evaluator = new Evaluator(
new CompilerContext(
new CompilerSettings
{
GenerateDebugInfo = true,
Optimize = true,
LoadDefaultReferences = !Host.IsWebApp()
},
new MonoReporter()));
if (references == null)
{
references = AppDomain.CurrentDomain.GetAssemblies()
.Where(
x => x.GetName()
.Name != "mscorlib");
}
references.ToList().ForEach(evaluator.ReferenceAssembly);
evaluator.Compile(sb.ToString());
evaluator.Run(code);
}
示例3: Execute
protected virtual ScriptResult Execute(string code, Evaluator session)
{
try
{
var parser = new SyntaxParser();
var parseResult = parser.Parse(code);
if (parseResult.TypeDeclarations != null && parseResult.TypeDeclarations.Any())
{
foreach (var @class in parseResult.TypeDeclarations)
{
session.Compile(@class);
}
}
if (parseResult.MethodExpressions != null && parseResult.MethodExpressions.Any())
{
foreach (var prototype in parseResult.MethodPrototypes)
{
session.Run(prototype);
}
foreach (var method in parseResult.MethodExpressions)
{
session.Run(method);
}
}
if (!string.IsNullOrWhiteSpace(parseResult.Evaluations))
{
object scriptResult;
bool resultSet;
session.Evaluate(parseResult.Evaluations, out scriptResult, out resultSet);
return new ScriptResult(returnValue: scriptResult);
}
}
catch (Exception ex)
{
return new ScriptResult(executionException: ex);
}
return ScriptResult.Empty;
}
示例4: CompileImpl
/// <exception cref="System.IO.IOException"></exception>
private object CompileImpl(Scriptable scope, TextReader sourceReader, string sourceString, string sourceName, int lineno, object securityDomain, bool returnFunction, Evaluator compiler, ErrorReporter compilationErrorReporter)
{
if (sourceName == null)
{
sourceName = "unnamed script";
}
if (securityDomain != null && GetSecurityController() == null)
{
throw new ArgumentException("securityDomain should be null if setSecurityController() was never called");
}
// One of sourceReader or sourceString has to be null
if (!(sourceReader == null ^ sourceString == null))
{
Kit.CodeBug();
}
// scope should be given if and only if compiling function
if (!(scope == null ^ returnFunction))
{
Kit.CodeBug();
}
CompilerEnvirons compilerEnv = new CompilerEnvirons();
compilerEnv.InitFromContext(this);
if (compilationErrorReporter == null)
{
compilationErrorReporter = compilerEnv.GetErrorReporter();
}
if (debugger != null)
{
if (sourceReader != null)
{
sourceString = Kit.ReadReader(sourceReader);
sourceReader = null;
}
}
Parser p = new Parser(compilerEnv, compilationErrorReporter);
if (returnFunction)
{
p.calledByCompileFunction = true;
}
AstRoot ast;
if (sourceString != null)
{
ast = p.Parse(sourceString, sourceName, lineno);
}
else
{
ast = p.Parse(sourceReader, sourceName, lineno);
}
if (returnFunction)
{
// parser no longer adds function to script node
if (!(ast.GetFirstChild() != null && ast.GetFirstChild().GetType() == Token.FUNCTION))
{
// XXX: the check just looks for the first child
// and allows for more nodes after it for compatibility
// with sources like function() {};;;
throw new ArgumentException("compileFunction only accepts source with single JS function: " + sourceString);
}
}
IRFactory irf = new IRFactory(compilerEnv, compilationErrorReporter);
ScriptNode tree = irf.TransformTree(ast);
// discard everything but the IR tree
p = null;
ast = null;
irf = null;
if (compiler == null)
{
compiler = CreateCompiler();
}
object bytecode = compiler.Compile(compilerEnv, tree, tree.GetEncodedSource(), returnFunction);
if (debugger != null)
{
if (sourceString == null)
{
Kit.CodeBug();
}
if (bytecode is DebuggableScript)
{
DebuggableScript dscript = (DebuggableScript)bytecode;
NotifyDebugger_r(this, dscript, sourceString);
}
else
{
throw new Exception("NOT SUPPORTED");
}
}
object result;
if (returnFunction)
{
result = compiler.CreateFunctionObject(this, scope, bytecode, securityDomain);
}
else
{
result = compiler.CreateScriptObject(bytecode, securityDomain);
}
return result;
}