本文整理汇总了C#中ScriptEngine.CreateScriptSourceFromString方法的典型用法代码示例。如果您正苦于以下问题:C# ScriptEngine.CreateScriptSourceFromString方法的具体用法?C# ScriptEngine.CreateScriptSourceFromString怎么用?C# ScriptEngine.CreateScriptSourceFromString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ScriptEngine
的用法示例。
在下文中一共展示了ScriptEngine.CreateScriptSourceFromString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PyMovingAverage
public PyMovingAverage()
{
HandlePythonExceptions(() =>
{
_engine = Python.CreateEngine();
var runtime = _engine.Runtime;
foreach (var reference in GetReferences())
runtime.LoadAssembly(reference);
string code = @"
import sys
sys.path.append(r'{0}')
from MovingAverage import MovingAverage
expert = MovingAverage()
";
code = string.Format(code, GetBasePath());
_scope = _engine.CreateScope();
var source = _engine.CreateScriptSourceFromString(code, SourceCodeKind.Statements);
source.Execute(_scope);
_expert = _scope.GetVariable("expert");
return 0;
});
}
示例2: Create
public ICondition Create()
{
_engine = Ruby.CreateEngine();
string rubyRuleTemplate;
using (var stream = Assembly.GetAssembly(this.GetType()).GetManifestResourceStream(_dslFileName))
{
using (var reader = new StreamReader(stream))
{
rubyRuleTemplate = reader.ReadToEnd();
}
}
rubyRuleTemplate = rubyRuleTemplate.Replace("$ruleAssembly$", _contextType.Namespace.Replace(".", "::"));
rubyRuleTemplate = rubyRuleTemplate.Replace("$contextType$", _contextType.Name);
rubyRuleTemplate = rubyRuleTemplate.Replace("$condition$", _condition);
_source = _engine.CreateScriptSourceFromString(rubyRuleTemplate);
var scope = _engine.CreateScope();
_assemblies.ForEach(a => _engine.Runtime.LoadAssembly(a));
_engine.Execute(_source.GetCode(), scope);
var @class = _engine.Runtime.Globals.GetVariable("RubyRuleFactory");
var installer = _engine.Operations.CreateInstance(@class);
var rule = installer.Create();
return (ICondition)rule;
}
示例3: IronRubyTemplateFactory
public IronRubyTemplateFactory( ScriptEngine scriptEngine, string className )
{
ScriptEngine = scriptEngine;
var format = string.Format("{0}.new.method(:render)", className);
var scriptSource = scriptEngine.CreateScriptSourceFromString(format);
RenderAction = scriptSource.Execute();
}
示例4: Py
public Py(Func<String> script)
{
_engine = ScriptRuntime.CreateFromConfiguration().GetEngine("python");
string pythonScript = script();
ScriptSource scriptSource = _engine.CreateScriptSourceFromString(pythonScript, SourceCodeKind.Statements);
_scope = _engine.CreateScope();
scriptSource.Execute(_scope);
}
示例5: IronPython
static IronPython()
{
pythonEngine = Python.CreateEngine();
pythonScope = pythonEngine.CreateScope();
var src = pythonEngine.CreateScriptSourceFromString(listisizeCode, SourceCodeKind.Statements);
src.Execute(pythonScope);
Listisize = pythonScope.GetVariable<Func<List<List<string>>, List<List<string>>>>("listisize");
}
示例6: PythonLoader
public PythonLoader(string code, string className = "PyClass")
{
//creating engine and stuff
engine = Python.CreateEngine();
scope = engine.CreateScope();
//loading and compiling code
source = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements);
compiled = source.Compile();
}
示例7: ScriptFilter
public ScriptFilter(string scriptText)
{
Validation.CheckObject("scriptText", scriptText);
//initialize python
runtime = Python.CreateRuntime();
engine = Python.GetEngine(runtime);
//load the script
source = engine.CreateScriptSourceFromString(scriptText);
script = source.Compile();
}
示例8: ReplWindow
public ReplWindow()
{
InitializeComponent();
_engine = Ruby.CreateEngine();
_scope = Ruby.CreateRuntime().CreateScope();
_scope.SetVariable("pi", Math.PI);
_scope.SetVariable("e", Math.E);
_scope.SetVariable("_", new Extension());
var source = _engine.CreateScriptSourceFromString("require 'RubyInt.exe'\nrequire 'Mathos.dll'\nrequire '" + Settings.DataDirectory + "Std.rb'", SourceCodeKind.Statements);
source.Execute(_scope);
}
示例9: PythonInstance
public PythonInstance(string code, string className = "PyClass")
{
//creating engine and stuff
engine = Python.CreateEngine();
scope = engine.CreateScope();
//loading and compiling codes
source = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements);
compiled = source.Compile();
//now executing this code (the code should contain a class)
compiled.Execute(scope);
//now creating an object that could be used to access the stuff inside a python script
pythonClass = engine.Operations.Invoke(scope.GetVariable(className));
}
示例10: Engine
public Engine(string source)
{
_engine = Python.CreateEngine();
_runtime = _engine.Runtime;
AddAssemblies();
PublishModule();
_scope = _engine.CreateScope();
_scope.SetVariable("__name__", "__main__");
Scope _main = HostingHelpers.GetScope(_scope);
_runtime.Globals.SetVariable("__main__", _main);
ScriptSource _script = _engine.CreateScriptSourceFromString(source, SourceCodeKind.Statements);
_code = _script.Compile();
}
示例11: ExecuteRuby
private void ExecuteRuby(string line, ScriptEngine engine, ScriptScope scope)
{
object result = null;
try
{
ScriptSource s = engine.CreateScriptSourceFromString(line, SourceCodeKind.AutoDetect);
result = s.Execute(scope);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
if (result != null)
Console.WriteLine("Result from IronRuby: " + result);
else
Console.WriteLine("Method returned nil");
}
示例12: PythonInstance
public PythonInstance(string className = "PyClass")
{
string code = @"
import sys
sys.path.append(r'C:\Program Files\IronPython 2.7\Lib')
import os
import hashlib
import urllib2
class PyClass:
def __init__(self):
pass
def somemethod(self):
print 'in some method'
def isodd(self, n):
return 1 == n % 2
def get_hash(self,name):
readsize = 64 * 1024
with open(name, 'rb') as f:
size = os.path.getsize(name)
data = f.read(readsize)
f.seek(-readsize, os.SEEK_END)
data += f.read(readsize)
return hashlib.md5(data).hexdigest()
";
//creating engine and stuff
engine = Python.CreateEngine();
scope = engine.CreateScope();
//loading and compiling code
source = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements);
compiled = source.Compile();
//now executing this code (the code should contain a class)
compiled.Execute(scope);
//now creating an object that could be used to access the stuff inside a python script
pythonClass = engine.Operations.Invoke(scope.GetVariable(className));
}
示例13: Create
public ICondition Create()
{
this.engine = Ruby.CreateEngine();
var rubyRuleTemplate = String.Empty;
using(var stream = new StreamReader(this.dslFileName))
{
rubyRuleTemplate = stream.ReadToEnd();
}
rubyRuleTemplate = rubyRuleTemplate.Replace("$ruleAssembly$", this.contextType.Namespace.Replace(".", "::"));
rubyRuleTemplate = rubyRuleTemplate.Replace("$contextType$", this.contextType.Name);
rubyRuleTemplate = rubyRuleTemplate.Replace("$condition$", this.condition);
this.source = engine.CreateScriptSourceFromString(rubyRuleTemplate);
var scope = engine.CreateScope();
assemblies.ForEach(a => engine.Runtime.LoadAssembly(a));
engine.Execute(source.GetCode(), scope);
var @class = engine.Runtime.Globals.GetVariable("RuleRuleFactory");
var installer = engine.Operations.CreateInstance(@class);
var rule = installer.Create();
return (ICondition) rule;
}
示例14: TestCategorizer
public void TestCategorizer(ScriptEngine engine, string src, int charCount, params TokenInfo[] expected) {
if (charCount == -1) charCount = src.Length;
TokenCategorizer categorizer = engine.GetService<TokenCategorizer>();
categorizer.Initialize(null, engine.CreateScriptSourceFromString(src), SourceLocation.MinValue);
IEnumerable<TokenInfo> actual = categorizer.ReadTokens(charCount);
int i = 0;
foreach (TokenInfo info in actual) {
Assert(i < expected.Length);
if (!info.Equals(expected[i])) {
Assert(false);
}
i++;
}
Assert(i == expected.Length);
}
示例15: SetScriptedSend
internal static string SetScriptedSend(ScriptEngine Engine, string Code)
{
try
{
ScriptRuntime Runtime = Engine.Runtime;
Assembly MainAssembly = Assembly.GetExecutingAssembly();
string RootDir = Directory.GetParent(MainAssembly.Location).FullName;
Runtime.LoadAssembly(MainAssembly);
Runtime.LoadAssembly(typeof(String).Assembly);
Runtime.LoadAssembly(typeof(Uri).Assembly);
if (Engine.Setup.DisplayName.Contains("IronPython"))
{
string[] Results = PluginEditor.CheckPythonIndentation(Code);
if (Results[1].Length > 0)
{
throw new Exception(Results[1]);
}
}
ScriptSource Source = Engine.CreateScriptSourceFromString(Code);
Source.ExecuteProgram();
return "";
}
catch (Exception Exp)
{
return Exp.Message;
}
}