About.core3.1 Dynamic Compiling

若汝棋茗 41 Reputation points
2021-03-17T14:50:13.483+00:00

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

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
749 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2021-03-18T02:41:34.373+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.