本文整理匯總了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
));
}