question

505554090 avatar image
0 Votes"
505554090 asked DanielZhang-MSFT edited

About.core3.1 Dynamic Compiling

Hi, I was compiling some code dynamically with Core3.1 and was told that the platform didn't support it, but using the Framework was fine.I don't know how to return a responsibility, ask everybody to help

 using RRQMSocket;
 using RRQMSocket.RPC;
 using System;
 using System.CodeDom.Compiler;
 using System.Collections.Generic;
 using System.IO;
 using System.Reflection;
    
 namespace RPCServiceCoreDemo
 {
     class Program
     {
           
         static void Main(string[] args)
         {
             var source = @"
                     using System;
                     namespace Sample 
                     { 
                         public class Program 
                         { 
                            public static void Main()
                             { 
                                 Console.WriteLine(""hello, world""); 
                             } 
                         } 
                     }";
    
             var a = GetAssemblyFromSourceByCodeDom(source);
              
             var methodA = a.CreateInstance("Sample.Program").GetType().GetMethod("Main");
              
             methodA.Invoke(null, null);
              
             Console.ReadKey();
         }
    
         static Assembly GetAssemblyFromSourceByCodeDom(params string[] source)
         {
             using var provider = CodeDomProvider.CreateProvider(
                                                         "CSharp",
                                                         new Dictionary<string, string> {
                                                             { "CompilerVersion", "v4.0" }});
    
             var compileResult = provider.CompileAssemblyFromSource(
                                                         new CompilerParameters()
                                                         {
                                                             IncludeDebugInformation = false,
                                                             TreatWarningsAsErrors = true,
                                                             WarningLevel = 4,
                                                             GenerateExecutable = false,
                                                             GenerateInMemory = true
                                                         },
                                                         source);
    
             if (compileResult.Errors.Count > 0)
             {
                 throw new ArgumentException();
             }
    
             return compileResult.CompiledAssembly;
         }
    
    
     }
 }

78819-image.png




dotnet-entity-framework-core
image.png (2.4 MiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT edited

Hi 505554090
From the documents below , we can see that the Microsoft .NET Framework exposes classes that allow you to programmatically access the C# language compiler.
And the System.CodeDom is not supported yet.
Compile code programmatically by using C# compiler
System.CodeDom
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.