1- using System ;
1+ using System ;
22using System . IO ;
33using System . Linq ;
44using System . Reflection ;
77
88namespace AssemblyImport . Tests
99{
10- public static class AssemblyGenerator
11- {
12- public static Assembly Generate ( string sourceFile )
13- {
14- var code = ReadFile ( sourceFile ) ;
15- var syntaxTree = CSharpSyntaxTree . ParseText ( code ) ;
16- var assemblyName = Path . GetFileNameWithoutExtension ( sourceFile ) ;
17- var assemblyPath = Path . Combine ( Path . GetTempPath ( ) , $ "{ assemblyName } .dll") ;
18-
19- var references = new MetadataReference [ ]
20- {
21- MetadataReference . CreateFromFile ( typeof ( object ) . Assembly . Location ) ,
22- MetadataReference . CreateFromFile ( typeof ( Enumerable ) . Assembly . Location )
23- } ;
10+ public static class AssemblyGenerator
11+ {
12+ public static Assembly Generate ( string sourceFile )
13+ {
14+ var code = ReadFile ( sourceFile ) ;
15+ var syntaxTree = CSharpSyntaxTree . ParseText ( code ) ;
16+ var assemblyName = Path . GetFileNameWithoutExtension ( sourceFile ) ;
17+ var assemblyPath = Path . Combine ( Path . GetTempPath ( ) , $ "{ assemblyName } .dll") ;
18+
19+ var references = new MetadataReference [ ]
20+ {
21+ MetadataReference . CreateFromFile ( typeof ( object ) . Assembly . Location ) ,
22+ MetadataReference . CreateFromFile ( typeof ( Enumerable ) . Assembly . Location )
23+ } ;
2424
25- if ( File . Exists ( assemblyPath ) )
26- {
27- File . Delete ( assemblyPath ) ;
28- }
25+ if ( File . Exists ( assemblyPath ) )
26+ {
27+ File . Delete ( assemblyPath ) ;
28+ }
2929
30- using ( var stream = File . Open ( assemblyPath , FileMode . OpenOrCreate , FileAccess . Write ) )
31- {
32- var compilation = CSharpCompilation . Create (
33- assemblyName ,
34- syntaxTrees : new [ ] { syntaxTree } ,
35- references : references ,
36- options : new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary ) ) ;
37-
38- var result = compilation . Emit ( stream ) ;
39- if ( ! result . Success )
40- {
41- var failures = result . Diagnostics . Where ( diagnostic =>
42- diagnostic . IsWarningAsError ||
43- diagnostic . Severity == DiagnosticSeverity . Error )
44- . Select ( diagnostic => new Exception ( $ "{ diagnostic . Id } : { diagnostic . GetMessage ( ) } ") )
45- . ToArray ( ) ;
30+ using ( var stream = File . Open ( assemblyPath , FileMode . OpenOrCreate , FileAccess . Write ) )
31+ {
32+ var compilation = CSharpCompilation . Create (
33+ assemblyName ,
34+ syntaxTrees : new [ ] { syntaxTree } ,
35+ references : references ,
36+ options : new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary ) ) ;
37+
38+ var result = compilation . Emit ( stream ) ;
39+ if ( ! result . Success )
40+ {
41+ var failures = result . Diagnostics . Where ( diagnostic =>
42+ diagnostic . IsWarningAsError ||
43+ diagnostic . Severity == DiagnosticSeverity . Error )
44+ . Select ( diagnostic => new Exception ( $ "{ diagnostic . Id } : { diagnostic . GetMessage ( ) } ") )
45+ . ToArray ( ) ;
4646
47- throw new AggregateException ( failures ) ;
48- }
49- }
50-
51- return Assembly . LoadFile ( assemblyPath ) ;
52- }
47+ throw new AggregateException ( failures ) ;
48+ }
49+ }
50+
51+ return Assembly . LoadFile ( assemblyPath ) ;
52+ }
5353
54- private static string ReadFile ( string fileName )
55- {
56- var assembly = Assembly . GetExecutingAssembly ( ) ;
57- var baseDirectory = Path . GetDirectoryName ( assembly . Location ) ;
58- var path = Path . Combine ( baseDirectory , "Resources" , fileName ) ;
59-
60- return File . ReadAllText ( path ) ;
61- }
62- }
63- }
54+ private static string ReadFile ( string fileName )
55+ {
56+ var assembly = Assembly . GetExecutingAssembly ( ) ;
57+ var baseDirectory = Path . GetDirectoryName ( assembly . Location ) ;
58+ var path = Path . Combine ( baseDirectory , "Resources" , fileName ) ;
59+
60+ return File . ReadAllText ( path ) ;
61+ }
62+ }
63+ }
0 commit comments