@@ -17,8 +17,8 @@ public class ExpressionCompiler
17
17
{
18
18
public List < ExpressionTranslator > Translators { get ; } = new List < ExpressionTranslator > ( ) ;
19
19
20
- private readonly ExpressionCompilationOptions _options ;
21
- public ExpressionCompiler ( ExpressionCompilationOptions options = null )
20
+ private readonly ExpressionCompilationOptions ? _options ;
21
+ public ExpressionCompiler ( ExpressionCompilationOptions ? options = null )
22
22
{
23
23
_options = options ;
24
24
}
@@ -35,10 +35,8 @@ public void AddFile(string code, string filename)
35
35
?? Path . Combine ( Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) , "GeneratedSources" ) ;
36
36
Directory . CreateDirectory ( root ) ;
37
37
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 ) ;
42
40
}
43
41
44
42
var sourceText = SourceText . From ( buffer , buffer . Length , Encoding . UTF8 , canBeEmbedded : true ) ;
@@ -53,12 +51,10 @@ public void AddFile(string code, string filename)
53
51
_codes . Add ( encoded ) ;
54
52
}
55
53
56
- public void AddFile ( LambdaExpression node , ExpressionDefinitions definitions = null )
54
+ public void AddFile ( LambdaExpression node , ExpressionDefinitions ? definitions = null )
57
55
{
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" ;
62
58
63
59
var translator = ExpressionTranslator . Create ( node , definitions ) ;
64
60
var script = translator . ToString ( ) ;
@@ -98,44 +94,42 @@ from n in t.TypeNames
98
94
. WithPlatform ( Platform . AnyCpu )
99
95
) ;
100
96
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 ) ;
107
102
108
- var embeddedTexts = _codes . Select ( it => EmbeddedText . FromSource ( it . FilePath , it . GetText ( ) ) ) ;
103
+ var embeddedTexts = _codes . Select ( it => EmbeddedText . FromSource ( it . FilePath , it . GetText ( ) ) ) ;
109
104
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 ) ;
115
110
116
- if ( ! result . Success )
117
- {
118
- var errors = new List < string > ( ) ;
111
+ if ( ! result . Success )
112
+ {
113
+ var errors = new List < string > ( ) ;
119
114
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 ) ;
123
118
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 ( ) } ") ;
126
121
127
- throw new Exception ( string . Join ( "\n " , errors ) ) ;
128
- }
122
+ throw new InvalidOperationException ( string . Join ( "\n " , errors ) ) ;
123
+ }
129
124
130
- assemblyStream . Seek ( 0 , SeekOrigin . Begin ) ;
131
- symbolsStream . Seek ( 0 , SeekOrigin . Begin ) ;
125
+ assemblyStream . Seek ( 0 , SeekOrigin . Begin ) ;
126
+ symbolsStream . Seek ( 0 , SeekOrigin . Begin ) ;
132
127
133
128
#if NETSTANDARD2_0
134
- return System . Runtime . Loader . AssemblyLoadContext . Default . LoadFromStream ( assemblyStream , symbolsStream ) ;
129
+ return System . Runtime . Loader . AssemblyLoadContext . Default . LoadFromStream ( assemblyStream , symbolsStream ) ;
135
130
#else
136
131
return Assembly . Load ( assemblyStream . ToArray ( ) , symbolsStream . ToArray ( ) ) ;
137
132
#endif
138
- }
139
133
}
140
134
141
135
}
0 commit comments