本文整理汇总了C#中System.Script.InitializeFromString方法的典型用法代码示例。如果您正苦于以下问题:C# Script.InitializeFromString方法的具体用法?C# Script.InitializeFromString怎么用?C# Script.InitializeFromString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Script
的用法示例。
在下文中一共展示了Script.InitializeFromString方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompilationPerfLargeMethod
private void CompilationPerfLargeMethod()
{
Console.WriteLine("Measuring compilation performance for a large method...");
const int totalCount = 100;
var sw = new Stopwatch();
// build the large methods ahead of time to not affect perf measurement
var methods = new List<string>();
for (var i = 0; i < totalCount; ++i)
{
var code = GetLargeMethod(200 + i);
methods.Add(code);
}
// compile a dummy script to force the initialization of the CLR and DLR
var dummyCode = GetSmallMethod(99);
var dummyCompiled = new Script();
dummyCompiled.InitializeFromString(dummyCode);
foreach (var code in methods)
{
var compiled = new Script();
sw.Start();
compiled.InitializeFromString(code);
sw.Stop();
}
Console.WriteLine(
string.Format("Compiled a total of {0} scripts, in {1} ms, averaging {2} ms per script.",
totalCount,
sw.ElapsedMilliseconds,
1.0 * sw.ElapsedMilliseconds / totalCount
));
}
示例2: SyntaxError
private void SyntaxError()
{
Console.WriteLine("Running script with syntax error...");
var codeWithSyntaxError = @"
class script3class:
def Message(self):
msg =! 'This has a syntax error!'
return message";
var compiled = new Script();
try
{
compiled.InitializeFromString(codeWithSyntaxError);
}
catch (Exception e)
{
Console.WriteLine(e.GetType() + " - " + e.Message);
Console.WriteLine("The formatted error message is:" + Environment.NewLine + compiled.LastError);
}
Console.WriteLine();
}
示例3: 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();
}
示例4: 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();
}
示例5: 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
));
}
示例6: CompilationPerfSmallMethod
private void CompilationPerfSmallMethod()
{
Console.WriteLine("Measuring compilation performance for a small method...");
const int totalCount = 100;
var sw = new Stopwatch();
// compile a dummy script to force the initialization of the CLR and DLR
var dummyCode = GetSmallMethod(99);
var dummyCompiled = new Script();
dummyCompiled.InitializeFromString(dummyCode);
for (var i = 0; i < totalCount; ++i)
{
var code = GetSmallMethod(100 + i);
var compiled = new Script();
sw.Start();
compiled.InitializeFromString(code);
sw.Stop();
}
Console.WriteLine(
string.Format("Compiled a total of {0} scripts, in {1} ms, averaging {2} ms per script.",
totalCount,
sw.ElapsedMilliseconds,
1.0 * sw.ElapsedMilliseconds / totalCount
));
}