Skip to content

Commit 29e96ba

Browse files
committed
support multiple methods
1 parent 70f5468 commit 29e96ba

9 files changed

+154
-151
lines changed

ExpressionDebugger.Tests/DebugInfoInjectorTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ public void TestExpression()
681681
Expression<Func<Data, Data>> lambda = data => new Data {Id = data.Id + "1", Records = data.Records.Select(it => it + 1)};
682682
var str = lambda.ToScript(new ExpressionDefinitions {IsExpression = true});
683683
Assert.AreEqual(@"
684-
public Expression<Func<DebugInfoInjectorTest.Data, DebugInfoInjectorTest.Data>> Main = data => new DebugInfoInjectorTest.Data()
684+
public Expression<Func<DebugInfoInjectorTest.Data, DebugInfoInjectorTest.Data>> Main => data => new DebugInfoInjectorTest.Data()
685685
{
686686
Id = data.Id + ""1"",
687687
Records = data.Records.Select<int, int>(it => it + 1)

ExpressionDebugger/ExpressionCompilationOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace ExpressionDebugger
55
{
66
public class ExpressionCompilationOptions
77
{
8-
public ExpressionDefinitions DefaultDefinitions { get; set; }
9-
public IEnumerable<Assembly> References { get; set; }
8+
public ExpressionDefinitions? DefaultDefinitions { get; set; }
9+
public IEnumerable<Assembly>? References { get; set; }
1010
public bool EmitFile { get; set; }
11-
public string RootPath { get; set; }
11+
public string? RootPath { get; set; }
1212
public bool? IsRelease { get; set; }
1313
public bool ThrowOnFailedCompilation { get; set; }
1414
}

ExpressionDebugger/ExpressionCompiler.cs

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public class ExpressionCompiler
1717
{
1818
public List<ExpressionTranslator> Translators { get; } = new List<ExpressionTranslator>();
1919

20-
private readonly ExpressionCompilationOptions _options;
21-
public ExpressionCompiler(ExpressionCompilationOptions options = null)
20+
private readonly ExpressionCompilationOptions? _options;
21+
public ExpressionCompiler(ExpressionCompilationOptions? options = null)
2222
{
2323
_options = options;
2424
}
@@ -35,10 +35,8 @@ public void AddFile(string code, string filename)
3535
?? Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "GeneratedSources");
3636
Directory.CreateDirectory(root);
3737
path = Path.Combine(root, filename);
38-
using (var fs = new FileStream(path, FileMode.Create))
39-
{
40-
fs.Write(buffer, 0, buffer.Length);
41-
}
38+
using var fs = new FileStream(path, FileMode.Create);
39+
fs.Write(buffer, 0, buffer.Length);
4240
}
4341

4442
var sourceText = SourceText.From(buffer, buffer.Length, Encoding.UTF8, canBeEmbedded: true);
@@ -53,12 +51,10 @@ public void AddFile(string code, string filename)
5351
_codes.Add(encoded);
5452
}
5553

56-
public void AddFile(LambdaExpression node, ExpressionDefinitions definitions = null)
54+
public void AddFile(LambdaExpression node, ExpressionDefinitions? definitions = null)
5755
{
58-
if (definitions == null)
59-
definitions = _options?.DefaultDefinitions ?? new ExpressionDefinitions { IsStatic = true };
60-
if (definitions.TypeName == null)
61-
definitions.TypeName = "Program";
56+
definitions ??= _options?.DefaultDefinitions ?? new ExpressionDefinitions {IsStatic = true};
57+
definitions.TypeName ??= "Program";
6258

6359
var translator = ExpressionTranslator.Create(node, definitions);
6460
var script = translator.ToString();
@@ -98,44 +94,42 @@ from n in t.TypeNames
9894
.WithPlatform(Platform.AnyCpu)
9995
);
10096

101-
using (var assemblyStream = new MemoryStream())
102-
using (var symbolsStream = new MemoryStream())
103-
{
104-
var emitOptions = new EmitOptions(
105-
debugInformationFormat: DebugInformationFormat.PortablePdb,
106-
pdbFilePath: symbolsName);
97+
using var assemblyStream = new MemoryStream();
98+
using var symbolsStream = new MemoryStream();
99+
var emitOptions = new EmitOptions(
100+
debugInformationFormat: DebugInformationFormat.PortablePdb,
101+
pdbFilePath: symbolsName);
107102

108-
var embeddedTexts = _codes.Select(it => EmbeddedText.FromSource(it.FilePath, it.GetText()));
103+
var embeddedTexts = _codes.Select(it => EmbeddedText.FromSource(it.FilePath, it.GetText()));
109104

110-
EmitResult result = compilation.Emit(
111-
peStream: assemblyStream,
112-
pdbStream: symbolsStream,
113-
embeddedTexts: embeddedTexts,
114-
options: emitOptions);
105+
EmitResult result = compilation.Emit(
106+
peStream: assemblyStream,
107+
pdbStream: symbolsStream,
108+
embeddedTexts: embeddedTexts,
109+
options: emitOptions);
115110

116-
if (!result.Success)
117-
{
118-
var errors = new List<string>();
111+
if (!result.Success)
112+
{
113+
var errors = new List<string>();
119114

120-
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
121-
diagnostic.IsWarningAsError ||
122-
diagnostic.Severity == DiagnosticSeverity.Error);
115+
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
116+
diagnostic.IsWarningAsError ||
117+
diagnostic.Severity == DiagnosticSeverity.Error);
123118

124-
foreach (Diagnostic diagnostic in failures)
125-
errors.Add($"{diagnostic.Id}: {diagnostic.GetMessage()}");
119+
foreach (Diagnostic diagnostic in failures)
120+
errors.Add($"{diagnostic.Id}: {diagnostic.GetMessage()}");
126121

127-
throw new Exception(string.Join("\n", errors));
128-
}
122+
throw new InvalidOperationException(string.Join("\n", errors));
123+
}
129124

130-
assemblyStream.Seek(0, SeekOrigin.Begin);
131-
symbolsStream.Seek(0, SeekOrigin.Begin);
125+
assemblyStream.Seek(0, SeekOrigin.Begin);
126+
symbolsStream.Seek(0, SeekOrigin.Begin);
132127

133128
#if NETSTANDARD2_0
134-
return System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromStream(assemblyStream, symbolsStream);
129+
return System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromStream(assemblyStream, symbolsStream);
135130
#else
136131
return Assembly.Load(assemblyStream.ToArray(), symbolsStream.ToArray());
137132
#endif
138-
}
139133
}
140134

141135
}

ExpressionDebugger/ExpressionDebugger.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
<SignAssembly>True</SignAssembly>
1313
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
1414
<AssemblyOriginatorKeyFile>ExpressionDebugger.snk</AssemblyOriginatorKeyFile>
15-
<Version>2.1.2</Version>
16-
<FileVersion>2.1.2</FileVersion>
17-
<AssemblyVersion>2.1.2</AssemblyVersion>
15+
<Version>2.2.0</Version>
1816
<PackageLicenseUrl>https://github.com/chaowlert/ExpressionDebugger/blob/master/LICENSE</PackageLicenseUrl>
17+
<LangVersion>8.0</LangVersion>
18+
<Nullable>enable</Nullable>
1919
</PropertyGroup>
2020

2121
<ItemGroup>

ExpressionDebugger/ExpressionDebuggerExtensions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using System.Reflection;
44

5+
// ReSharper disable once CheckNamespace
56
namespace System.Linq.Expressions
67
{
78
public static class ExpressionDebuggerExtensions
@@ -41,20 +42,21 @@ public static Delegate CompileWithDebugInfo(this LambdaExpression node, Expressi
4142

4243
public static Delegate CreateDelegate(this ExpressionTranslator translator, Assembly assembly)
4344
{
44-
var definitions = translator.Definitions;
45+
var definitions = translator.Definitions!;
4546
var typeName = definitions.Namespace == null
4647
? definitions.TypeName
47-
: (definitions.Namespace + "." + definitions.TypeName);
48+
: definitions.Namespace + "." + definitions.TypeName;
4849
var type = assembly.GetType(typeName);
49-
var method = type.GetMethod(definitions.MethodName ?? "Main");
50+
var main = translator.Methods.First();
51+
var method = type.GetMethod(main.Key);
5052
var obj = definitions.IsStatic ? null : Activator.CreateInstance(type);
5153
var flag = definitions.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
5254
foreach (var kvp in translator.Constants)
5355
{
5456
var field = type.GetField(kvp.Value, BindingFlags.NonPublic | flag);
5557
field.SetValue(obj, kvp.Key);
5658
}
57-
return method.CreateDelegate(translator.Expression.Type, obj);
59+
return method.CreateDelegate(main.Value, obj);
5860
}
5961
}
6062
}

ExpressionTranslator/ExpressionDefinitions.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33

44
namespace ExpressionDebugger
55
{
6-
public class ExpressionDefinitions
6+
public class ExpressionDefinitions : TypeDefinitions
77
{
8-
public string Namespace { get; set; }
9-
public string TypeName { get; set; }
10-
public bool IsStatic { get; set; }
11-
public string MethodName { get; set; }
12-
public IEnumerable<Type> Implements { get; set; }
8+
public string? MethodName { get; set; }
139
public bool IsExpression { get; set; }
1410
}
11+
12+
public class TypeDefinitions
13+
{
14+
public string? Namespace { get; set; }
15+
public string? TypeName { get; set; }
16+
public bool IsStatic { get; set; }
17+
public IEnumerable<Type>? Implements { get; set; }
18+
}
19+
1520
}

0 commit comments

Comments
 (0)