![]() ![]() MetadataReference.CreateFromFile( typeof(Console).GetTypeInfo().Assembly.Location) MetadataReference.CreateFromFile( typeof( object).GetTypeInfo().Assembly.Location), I used the same approach as before to add it: var compilation = CSharpCompilation.Create(Path.GetFileName(assemblyPath)) For starters, there was another missing assembly reference: (9,13): error CS0103: The name 'Console' does not exist in the current context My code built the executable and ran it.NET Core NET framework, that was all I needed to get it working. MetadataReference.CreateFromFile( typeof( object).GetTypeInfo().Assembly.Location) Adding the assembly containing one of the above-mentioned types was enough to fix it: var compilation = CSharpCompilation.Create(Path.GetFileName(assemblyPath)) It turned out that all of them were caused by a missing reference to the mscorlib.dll assembly. (5,18): error CS1729: 'object' does not contain a constructor that takes 0 arguments (10,13): error CS0103: The name 'Console' does not exist in the current context (10,13): error CS0518: Predefined type 'System.Object' is not defined or imported (9,27): error CS0518: Predefined type 'System.String' is not defined or imported ![]() ![]() (9,13): error CS0103: The name 'Console' does not exist in the current context (9,13): error CS0518: Predefined type 'System.Object' is not defined or imported (7,16): error CS0518: Predefined type 'System.Void' is not defined or imported (7,26): error CS0518: Predefined type 'System.String' is not defined or imported (5,18): error CS0518: Predefined type 'System.Object' is not defined or imported Unfortunately, the build failed with a rather long list of diagnostic errors: (1,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) Var result = compilation.Emit(assemblyPath) WithOptions( new CSharpCompilationOptions(OutputKind.ConsoleApplication)) Var compilation = CSharpCompilation.Create(Path.GetFileName(assemblyPath)) Var assemblyPath = Path.ChangeExtension(Path.GetTempFileName(), "exe") I just needed to find the correct sequence of API calls to invoke the compiler: var syntaxTree = SyntaxFactory.ParseSyntaxTree(SourceText.From(sourceCode)) The basic setup is pretty straightforward. I was wondering how difficult it would be to use it to compile some C# source code into an executable and run it.īeing able to compile the following source code into a console application would be a good start: using System Since the release of Roslyn, the complete C# compiler pipeline is available as a NuGet package and we can include it in our own application. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |