本文整理汇总了C#中System.Script.Execute方法的典型用法代码示例。如果您正苦于以下问题:C# Script.Execute方法的具体用法?C# Script.Execute怎么用?C# Script.Execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Script
的用法示例。
在下文中一共展示了Script.Execute方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public dynamic Execute(Script script)
{
if (script == null)
throw new ArgumentNullException("script");
return script.Execute(this.executionContext);
}
示例2: ThrowActualExceptionInsteadOfTargetInvocationException
public void ThrowActualExceptionInsteadOfTargetInvocationException()
{
Script task = new Script();
task.BuildEngine = new MockBuild();
task.Code = @"public static void ScriptMain(){
throw new InvalidOperationException(""This is the actual exception."");
}";
Assert.IsTrue(task.Execute(), "Task should have succeeded.");
}
示例3: Execute
public void Execute(Script script)
{
if (script == null)
{
throw new ArgumentNullException("script");
}
script.Execute();
}
示例4: AttemptsToReturnANonStringValue
public void AttemptsToReturnANonStringValue()
{
Script task = new Script();
task.BuildEngine = new MockBuild();
task.Code = @"public static int ScriptMain(){
return 4;
}";
Assert.IsTrue(task.Execute(), "Task should have succeeded.");
Assert.IsNull(task.ReturnValue, "ReturnValue should only be set when it is a string.");
}
示例5: SetsReturnValue
public void SetsReturnValue()
{
Script task = new Script();
task.BuildEngine = new MockBuild();
task.Code = @"public static string ScriptMain(){
return ""Hello World"";
}";
Assert.IsTrue(task.Execute(), "Task should have succeeded.");
Assert.AreEqual("Hello World", task.ReturnValue, "ReturnValue should have been set.");
}
示例6: Execute
public void Execute()
{
Script task = new Script();
task.BuildEngine = new MockBuild();
task.Code = @"public static void ScriptMain(){
int x = 1;
System.Diagnostics.Debug.WriteLine(x);
}";
Assert.IsTrue(task.Execute(), "Task should have succeeded.");
}
示例7: ExecuteWithStrongNameReference
public void ExecuteWithStrongNameReference()
{
Script task = new Script();
task.BuildEngine = new MockBuild();
task.Code = @"public static void ScriptMain(){
string x = System.Web.HttpUtility.HtmlEncode(""<tag>"");
System.Diagnostics.Debug.WriteLine(x);
}";
task.References = new ITaskItem[] { new TaskItem("System.Web") };
Assert.IsTrue(task.Execute(), "Task should have succeeded.");
}
示例8: ExecuteWithImports
public void ExecuteWithImports()
{
Script task = new Script();
task.BuildEngine = new MockBuild();
task.Code = @"public static void ScriptMain(){
int x = 1;
Debug.WriteLine(x);
}";
task.Imports = new ITaskItem[] { new TaskItem("System.Diagnostics") };
Assert.IsTrue(task.Execute(), "Task should have succeeded.");
}
示例9: InitScript
public string InitScript()
{
Script = new Script();
if (Settings.Script.Length > 0)
{
try
{
Script.GlobalsAdd("MGO", MGO);
Script.GlobalsAdd("World", this);
Script.GlobalsAdd("Session", session);
Script.GlobalsAdd("AiEventHandler", aiEventHandler);
Script.GlobalsAdd("GetFrameFactor", new GameAI.GetFrameFactorDelegate(GameAI.GetFrameFactor));
Script.Execute("lr = littleRunner(MGO, World, Session, AiEventHandler, GetFrameFactor)");
Script.GlobalsAdd("lr");
foreach (GameObject go in this.AllElements)
{
if (go.Name != null && go.Name != "")
{
Script.GlobalsAdd(go.Name, go);
Script.Execute("lr.Handler." + go.Name + " = EventAttrDict()");
}
}
Script.Execute("handler = lr.Handler"); // very important! because Script.cs needs access to Globals["handler"].
Script.GlobalsAdd("handler");
Script.Execute(Settings.Script);
Script.Init = false;
}
catch (NullReferenceException)
{
return null; // Script closed game!
}
catch (Exception e)
{
return e.GetType().FullName + ":\n" + e.Message;
}
}
return "";
}
示例10: ScriptInString
private void ScriptInString()
{
Console.WriteLine("Running script from string...");
var code = @"
class script2class:
def Goodbye(self):
message = 'Goodbye via the script!'
return message
def GoodbyeWithName(self, name):
message = 'Goodbye ' + name
return message
def GoodbyeWithPerson(self, person):
message = 'Goodbye ' + person.Name
return message";
var compiled = new Script();
compiled.InitializeFromString(code);
Console.WriteLine(compiled.Execute<string>("script2class", "Goodbye"));
// single param call
Console.WriteLine(compiled.Execute<string>("script2class", "GoodbyeWithName", "Paulo"));
// pass instance of complex type
var person = new Person { Name = "Paulo Mouat", Address = "Boston" };
Console.WriteLine(compiled.Execute<string>("script2class", "GoodbyeWithPerson", person));
// duck typing
var hasNameLikePerson = new HasNameLikePerson { Name = "HAL", Model = 9000 };
Console.WriteLine(compiled.Execute<string>("script2class", "GoodbyeWithPerson", hasNameLikePerson));
Console.WriteLine();
}
示例11: ScriptInFile
private void ScriptInFile()
{
Console.WriteLine("Running script from file...");
var script = new Script();
script.InitializeFromFile(@"..\..\..\Scripts\script1.py");
// no params call
Console.WriteLine(script.Execute<string>("script1class", "Hello"));
// single param call
Console.WriteLine(script.Execute<string>("script1class", "HelloWithName", "Paulo"));
// pass instance of complex type
var person = new Person { Name = "Paulo Mouat", Address = "Boston" };
Console.WriteLine(script.Execute<string>("script1class", "HelloWithPerson", person));
// duck typing
var hasNameLikePerson = new HasNameLikePerson { Name = "HAL", Model = 9000 };
Console.WriteLine(script.Execute<string>("script1class", "HelloWithPerson", hasNameLikePerson));
// change field in passed type
var changeState = new Person { Name = "Paulo Mouat", Address = "Boston" };
Console.WriteLine("Original state: " + changeState);
script.Execute<string>("script1class", "ChangeAddress", changeState);
Console.WriteLine("New state: " + changeState);
Console.WriteLine();
}
示例12: RuntimeError
private void RuntimeError()
{
Console.WriteLine("Running script with runtime error...");
var codeWithRuntimeError = @"
class script4class:
def Message(self):
msg = 1/0
return msg";
var compiled = new Script();
try
{
compiled.InitializeFromString(codeWithRuntimeError);
compiled.Execute<string>("script4class", "Message");
}
catch (Exception e)
{
Console.WriteLine(e.GetType() + " - " + e.Message);
}
Console.WriteLine();
}
示例13: ExecutionPerf
private void ExecutionPerf()
{
Console.WriteLine("Measuring execution performance for a large method...");
const int totalCount = 10000;
var sw = new Stopwatch();
const int id = 300;
var code = GetLargeMethod(id);
var compiled = new Script();
compiled.InitializeFromString(code);
for (var i = 0; i < totalCount; ++i)
{
sw.Start();
compiled.Execute<string>("script300class", "ManyIfStatements", i);
sw.Stop();
}
Console.WriteLine(
string.Format("Executed script a total of {0} times, in {1} ms, averaging {2} ms per run.",
totalCount,
sw.ElapsedMilliseconds,
1.0 * sw.ElapsedMilliseconds / totalCount
));
}