📜 ⬆️ ⬇️

Runtime-generating .Net code for those who have no time

The .Net infrastructure contains built-in code generation (On-the-Fly Code Generation). This allows the .Net program to compile the text written in any programming language and execute the resulting code at the time of its execution itself (without the participation of the programmer). It would be logical to expect that the simplest method of the corresponding class is provided for the implementation of these actions in the standard .Net library. But unfortunately it is not. Microsoft, having done a tremendous way of embedding code generation tools on .Net, did not take the very last step towards the simplest needs of programmers. So you have to do this step yourself.

The simplest means offered by Microsoft for solving the described task is the CSharpCodeProvider class, which is included in the .Net standard library. Using this class is not a very difficult task, but nevertheless it would be useful to have a tool that turns the use of code generated on the fly into a trivial task.


The result is a small set of classes, of which Tech.DynamicCoding.CodeGenerator is Tech.DynamicCoding.CodeGenerator . Let's begin its description with the simplest example of use. (Texts of the library and examples are in the archive ).
')

Generating the simplest code


Suppose you need to calculate the value of a numeric expression given in text form. For example, here is such a "12345678 * 9 + 9". In this case, you just need to write the following:

 var result = CodeGenerator.ExecuteCode<int>("return 12345678 * 9 + 9;"); 

At first, you create a fragment of C # code and pass it as a parameter to the CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode . , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
method CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode . , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode . , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
 CodeGenerator.ExecuteCode.        - .  ,        .  ?  ? 


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
 CodeGenerator.ExecuteCode.        - .  ,        .  ?  ? 


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
 CodeGenerator.ExecuteCode.        - .  ,        .  ?  ? 


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
 CodeGenerator.ExecuteCode.        - .  ,        .  ?  ? 


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
 CodeGenerator.ExecuteCode.        - .  ,        .  ?  ? 


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
 CodeGenerator.ExecuteCode.        - .  ,        .  ?  ? 


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
 CodeGenerator.ExecuteCode.        - .  ,        .  ?  ? 


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it
CodeGenerator.ExecuteCode. - . , . ? ?


. " ", " " ExecuteCode
. , , , , . , , , . , , , , .

, . .

var result = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 9876543.21), CodeParameter.Create("b", 9), CodeParameter.Create("c", -0.01)); var result2 = CodeGenerator.ExecuteCode<double>("return a * b + c;", CodeParameter.Create("a", 12345678.9), CodeParameter.Create("b", 8), CodeParameter.Create("c", 0.9));
, , . ExecuteCode , , . C#-, , , ExecuteCode .


. , .

var code = CodeGenerator.CreateCode<DateTime>( "return StartDate.AddDays(Duration);", new CodeParameter("StartDate", typeof(DateTime)), new CodeParameter("Duration", typeof(int))); var result1 = code.Execute(DateTime.Parse("2013-01-01"), 256); var result2 = code.Execute(DateTime.Parse("2013-10-13"), 131);
. C#-, , . . .

. .

. , , . , - . , . , .

- . , . (, ). , .


, (AppDomain) - "" (sandbox). , . , , .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, "return (int)(DateTime.Now - StartDate).TotalDays;", new CodeParameter("StartDate", typeof(DateTime))); var result = code.Execute(DateTime.Parse("1962-09-17")); }
Sandbox , AppDomain. . .


, , . - .

using (var sandbox = new Sandbox()) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(@"c:\temp\a.txt"); // SecurityException }
, . .

const string FILE_PATH = @"c:\temp\a.txt"; using (var sandbox = new Sandbox( new FileIOPermission(FileIOPermissionAccess.AllAccess, FILE_PATH))) { var code = CodeGenerator.CreateCode<int>(sandbox, @"System.IO.File.Delete(filePath);", new CodeParameter("filePath", typeof(string))); code.Execute(FILE_PATH); }

,
. , . , .. .

, . , MarshalByRefObject. .Net Remoting, .

. :

public class AirConditioner : MarshalByRefObject { public bool Working { get; set; } } [Serializable] public struct Climate { public double Temperature { get; set; } }
, / , .

const string controlAlgorithm = @" IF Climate.Temperature > 26 THEN Unit.Working = TRUE IF Climate.Temperature < 22 THEN Unit.Working = FALSE"; var unit = new AirConditioner(); var limate = new Climate { Temperature = 28 }; using (var sandbox = new Sandbox()) { var controlCode = CodeGenerator.CreateCode<int>(sandbox, VB.Compiler, controlAlgorithm, null, null, CodeParameter.Create("Unit", unit), CodeParameter.Create("Climate", limate)); while (!Console.KeyAvailable) { Console.WriteLine("t={0}°C", limate.Temperature); controlCode.Execute(unit, limate); Thread.Sleep(300); limate.Temperature += unit.Working ? -1 : 1; } }
, , MarshalByRefObject, Climate [Serializable].


.

1. #- System , , . C# using'. . , , . .

2. System.dll mscorlib.dll, . , . , .

, , . .

3. C#. . VB.Net. . ( .)


, , . . .

, . , , -, . , - (plugin modules).

. , . , .



, , , .


C#

C# " "

CS-Script - The C# Script Engine
Security and On-the-Fly Code Generation
How to: Run Partially Trusted Code in a Sandbox
Metaprogramming in .NET. EBook
Debugging a generated .NET assembly from within the application that generated it

Source: https://habr.com/ru/post/199266/


All Articles