Skip to content

Commit f31f348

Browse files
committed
Refactored SimpleDynamicCompiler.
1 parent 3bf74e4 commit f31f348

3 files changed

Lines changed: 12 additions & 19 deletions

File tree

DynamicCode/Compiler/Constants.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace DynamicCode.Compiler;
2+
3+
internal class Constants
4+
{
5+
internal const string AssemblyName = "DynamicAssembly";
6+
}

DynamicCode/Compiler/DynamicCompiler.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ namespace DynamicCode.Compiler;
66

77
public class DynamicCompiler
88
{
9-
private const string AssemblyName = "DynamicAssembly";
10-
119
public static Delegate CompileFunction(Type delegateType, string code)
1210
{
1311
var syntaxTree = CSharpSyntaxTree.ParseText(code);
@@ -20,7 +18,7 @@ public static Delegate CompileFunction(Type delegateType, string code)
2018
.Cast<MetadataReference>();
2119

2220
var compilation = CSharpCompilation.Create(
23-
assemblyName: AssemblyName,
21+
assemblyName: Constants.AssemblyName,
2422
syntaxTrees: [syntaxTree],
2523
references: references,
2624
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)

DynamicCode/Compiler/SimpleDynamicCompiler.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,16 @@ namespace DynamicCode.Compiler;
66

77
public class SimpleDynamicCompiler
88
{
9-
public static Func<int, int, int> CompileFunction(string body)
9+
public static T CompileFunction<T>(string code, string className, string methodName) where T : Delegate
1010
{
11-
// Source code for a class with a static method Calculate
12-
string code = $@"
13-
using System;
14-
public static class DynamicClass
15-
{{
16-
public static int Calculate(int x, int y)
17-
{{
18-
{body}
19-
}}
20-
}}";
21-
2211
var syntaxTree = CSharpSyntaxTree.ParseText(code);
2312
var references = AppDomain.CurrentDomain.GetAssemblies()
2413
.Where(a => !a.IsDynamic && !string.IsNullOrEmpty(a.Location))
2514
.Select(a => MetadataReference.CreateFromFile(a.Location))
2615
.Cast<MetadataReference>();
2716

2817
var compilation = CSharpCompilation.Create(
29-
assemblyName: "DynamicAssembly",
18+
assemblyName: Constants.AssemblyName,
3019
syntaxTrees: [syntaxTree],
3120
references: references,
3221
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
@@ -42,9 +31,9 @@ public static int Calculate(int x, int y)
4231

4332
ms.Seek(0, SeekOrigin.Begin);
4433
var asm = Assembly.Load(ms.ToArray());
45-
var type = asm.GetType("DynamicClass");
46-
var method = type!.GetMethod("Calculate", BindingFlags.Public | BindingFlags.Static);
34+
var type = asm.GetType(className);
35+
var method = type!.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
4736

48-
return (Func<int, int, int>)method!.CreateDelegate(typeof(Func<int, int, int>));
37+
return (T)method!.CreateDelegate(typeof(T));
4938
}
5039
}

0 commit comments

Comments
 (0)