本文整理汇总了C#中ScriptEngine.Evaluate方法的典型用法代码示例。如果您正苦于以下问题:C# ScriptEngine.Evaluate方法的具体用法?C# ScriptEngine.Evaluate怎么用?C# ScriptEngine.Evaluate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ScriptEngine
的用法示例。
在下文中一共展示了ScriptEngine.Evaluate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CastDoubleToString
public void CastDoubleToString()
{
var script = new ScriptEngine();
Assert.AreEqual("4", script.Evaluate<string>("string(4.0)"));
Assert.AreEqual("4.2", script.Evaluate<string>("string(4.2)"));
Assert.AreEqual("4.2", script.Evaluate<string>("string(4.20)"));
}
示例2: RequireJsCompiler
public RequireJsCompiler(TextWriter consoleOut, string currentDirectory)
{
_jsEngine = new ScriptEngine();
_jsEngine.CompatibilityMode = CompatibilityMode.ECMAScript3;
_jsEngine.Evaluate(LoadResource("require.js"));
_jsEngine.Evaluate(LoadResource("json2.js"));
_jsEngine.Evaluate(LoadResource(@"adapt\rhino.js"));
_jsEngine.Evaluate("require(" + new JavaScriptSerializer().Serialize(new {
baseUrl = ResourceBaseUrl
}) + ");");
_fs = new NativeFS(_jsEngine, currentDirectory);
_jsEngine.SetGlobalValue("YUNoFS", _fs);
_path = new NativePath(_jsEngine, currentDirectory);
_jsEngine.SetGlobalValue("YUNoPath", _path);
_ioAdapter = new IOAdapter(_jsEngine, consoleOut);
_jsEngine.SetGlobalFunction("load", (Action<string>)_ioAdapter.load);
_jsEngine.SetGlobalValue("ioe", _ioAdapter);
_jsEngine.SetGlobalValue("IsRunningYUNoAMD", true);
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "env.js", @"build\jslib\env.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "yunoamd/args.js", @"build\jslib\yunoamd\args.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "build.js", @"build\build.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "print.js", @"build\jslib\yunoamd\print.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "fs.js", @"build\jslib\yunoamd\fs.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "path.js", @"build\jslib\yunoamd\path.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "file.js", @"build\jslib\node\file.js");
}
示例3: Scriptable
public Scriptable()
{
engine = new ScriptEngine();
engine.Evaluate("funcccc = function() { return 3 * 3; }");
object funccc = engine.GetGlobalValue("funcccc");
engine.Evaluate("funcccc = function() { return 9 * 9; }");
loopFuncs = new List<MGFunction>();
timeAtLastUpdate = System.DateTime.Now;
}
示例4: GetMissingPropertyValue
public void GetMissingPropertyValue()
{
var engine = new ScriptEngine();
// Create a new TestClass1 instance.
engine.SetGlobalValue("test", new TestClass1(engine));
// Missing properties should return the name of the property.
Assert.AreEqual("unknown", engine.Evaluate("test.unknown"));
Assert.AreEqual("unknown", engine.Evaluate("test['unknown']"));
Assert.AreEqual("1", engine.Evaluate("test[1]"));
// Existing properties should still work.
Assert.AreEqual(1, engine.Evaluate("test.exists"));
// Missing properties return null for Object.getOwnPropertyDescriptor().
Assert.AreEqual(Undefined.Value, engine.Evaluate("Object.getOwnPropertyDescriptor(test, 'unknown')"));
// Missing properties return false from hasOwnProperty().
Assert.AreEqual(false, engine.Evaluate("test.hasOwnProperty('unknown')"));
// Missing properties return false from propertyIsEnumerable().
Assert.AreEqual(false, engine.Evaluate("test.propertyIsEnumerable('unknown')"));
// Create a new TestClass2 instance.
engine.SetGlobalValue("test", new TestClass2(engine));
// Missing properties should return a function which can be called.
Assert.AreEqual("unknown0", engine.Evaluate("test.unknown()"));
Assert.AreEqual("unknown1", engine.Evaluate("test.unknown(1)"));
}
示例5: SetGlobalFunction
public void SetGlobalFunction()
{
var engine = new ScriptEngine();
// Try a static delegate.
engine.SetGlobalFunction("add", new Func<int, int, int>((a, b) => a + b));
Assert.AreEqual(11, engine.Evaluate("add(5, 6)"));
// Try an instance delegate.
engine.SetGlobalFunction("global", new Func<object>(() => engine.Global));
Assert.AreEqual(5, engine.Evaluate("global().parseInt('5')"));
}
示例6: Evaluate
public string Evaluate(string javascriptExpression, ScenarioContext scenarioContext)
{
var engine = new ScriptEngine();
string dependencies = GetDependencies(scenarioContext);
string javascriptToEval = string.Format("var out; {0}; out = {1};", dependencies, javascriptExpression);
object returnVal = engine.Evaluate(javascriptToEval);
if (returnVal is UserDefinedFunction)
{
engine.Execute(javascriptToEval);
return engine.Evaluate("out()").ToString();
}
return returnVal.ToString();
}
示例7: Specify
public override void Specify()
{
it(".NET methods evaluate scripts within their current context", delegate()
{
var engine = new ScriptEngine();
var evaluator = new NestingEvaluator(engine);
engine.SetGlobalValue("foo", evaluator);
var result1 = engine.Evaluate("var x = 16; (function(x) { return foo.evaluateAndDouble1('x');})(2)");
var result2 = engine.Evaluate("var x = 16; (function(x) { return foo.evaluateAndDouble2('x');})(2)");
expect(() => (int)result1 == (int)result2 && (int)result1 == 4);
});
}
示例8: PropertyDouble
public void PropertyDouble()
{
var script = new ScriptEngine();
var test = script.AddClass("Test");
test.AddProperty("test", 10.0);
Assert.AreEqual(10.0, script.Evaluate<double>("Test.test"));
}
示例9: PropertyBoolTrue
public void PropertyBoolTrue()
{
var script = new ScriptEngine();
var test = script.AddClass("Test");
test.AddProperty("test", true);
Assert.AreEqual(true, script.Evaluate<bool>("Test.test"));
}
示例10: ReadAllTasks
public static ICollection<string> ReadAllTasks(string path)
{
var list = new List<string>();
if (path.EndsWith(".ts"))
{
path = path.Replace(".ts", ".js").Replace(".coffee",".js");
}
if (!File.Exists(path))
{
return list;
}
// executes the gulpfile with some little additions :)
try
{
var engine = new ScriptEngine();
engine.Execute(Resources.gulpInit);
engine.ExecuteFile(path);
// reads the evaluated tasks
ArrayInstance names = (ArrayInstance)engine.Evaluate("tasks");
foreach (var elem in names.ElementValues)
{
list.Add((string)elem);
}
}
catch (Exception)
{
list.Add("Cannot parse gulpfile");
}
return list.Distinct().ToList();
}
示例11: PropertyString
public void PropertyString()
{
var script = new ScriptEngine();
var test = script.AddClass("Test");
test.AddProperty("test", "Hello World!");
Assert.AreEqual("Hello World!", script.Evaluate<string>("Test.test"));
}
示例12: PropertyInteger
public void PropertyInteger()
{
var script = new ScriptEngine();
var test = script.AddClass("Test");
test.AddProperty("test", 10);
Assert.AreEqual(10, script.Evaluate<int>("Test.test"));
}
示例13: Execute
public ActionResult Execute()
{
try
{
var script = ReadScriptFromRequestBody();
// TODO: Check that script is a "function() { ... }".
var scriptEngine = new ScriptEngine();
ConfigureScriptEngine(scriptEngine);
var result = scriptEngine.Evaluate("(" + script + "())");
if (result == Undefined.Value)
{
Response.ContentType = "application/json";
return new EmptyResult();
}
// TODO: Check for non-primitive results and convert into a serialization friendly format.
// However, the result is probably only ever undefined or a boolean. So don't worry for now.
return Json(result);
}
catch (Exception ex)
{
Response.StatusCode = 500;
return Content(ex.ToString(), "text/plain");
}
}
示例14: ReadAllTasks
/// <summary>
/// Reads all the defined tasks in a Gruntfile whose path is passed as parameter
/// </summary>
/// <param name="path">The path of the file to examine</param>
/// <returns>A list of tasks</returns>
public static ICollection<string> ReadAllTasks(string path)
{
var list = new List<string>();
//executes the gruntfile with some little additions :)
try
{
var engine = new ScriptEngine();
engine.Execute(Resources.Init);
engine.ExecuteFile(path);
engine.Execute("module.exports(grunt);");
//reads the evaluated tasks
ArrayInstance names = (ArrayInstance)engine.Evaluate("names");
foreach (var elem in names.ElementValues)
{
list.Add((string)elem);
}
}
catch (Exception) {
list.Add("Cannot parse Gruntfile");
}
return list;
}
示例15: BigNumber
public void BigNumber()
{
var engine = new ScriptEngine();
engine.SetGlobalValue("console", new Jurassic.Library.FirebugConsole(engine));
engine.ExecuteFile(@"..\..\..\Unit Tests\Real-world\Files\bignumber.js");
engine.EnableDebugging = true;
Assert.AreEqual("129963294281830400", engine.Evaluate(@"new BigNumber('129963294281830401').subtract(1).toString()"));
}