dotNetCore动态编译代码为什么会出现这样的错误?

本应该多点分数打赏,但实在无耐分数有限。望各位不啬指教,非常感谢!
开发环境
donNetCore v2.1

Microsoft.CodeAnalysis.Compilers v2.8.0

动态编译代码:

StringBuilder code = new StringBuilder();
code.AppendLine("namespace DotNetCoreTest");
code.AppendLine("{");
code.AppendLine("    public class TestCode");
code.AppendLine("    {");
code.AppendLine("        public int test(int a)");
code.AppendLine("        {");
code.AppendLine("            a++;");
code.AppendLine("            return a;");
code.AppendLine("        }");
code.AppendLine("    }");
code.AppendLine("}");

string sysFile = typeof(Enumerable).Assembly.Location;
string sysDir = Directory.GetParent(sysFile).FullName;

List<MetadataReference> references = new List<MetadataReference>();
references.Add(MetadataReference.CreateFromFile(Path.Combine(sysDir, "System.dll")));
references.Add(MetadataReference.CreateFromFile(Path.Combine(sysDir, "System.Xml.dll")));

string assemblyName = "DotNetCoreTest";
SyntaxTree tree = SyntaxFactory.ParseSyntaxTree(code.ToString());
CSharpCompilation compilation = CSharpCompilation.Create(assemblyName);
compilation.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Debug));
compilation.AddReferences(references);
compilation.AddSyntaxTrees(tree);

string targetPath = "D:\\test.dll";
EmitResult compilationResult = compilation.Emit(targetPath);

string err = "";
if (!compilationResult.Success)
{
    foreach (Diagnostic item in compilationResult.Diagnostics)
    {
        err += "\r\nid: " + item.Id + "\r\nerror: " + item.GetMessage() + "\r\nlocation: " + item.Location.GetLineSpan().ToString();
    }
}

执行以上代码出现如下错误信息:
id: CS8021
error: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
location: : (0,0)-(0,0)
id: CS5001
error: Program does not contain a static 'Main' method suitable for an entry point
location: : (0,0)-(0,0)

请教各位大牛,错误信息里提示 RuntimeMetadataVersion 没有值,但是我又没找到在哪里可以设置这个值的地方,这如何解决?

还有个错误提示说没有 Main 方法,我动态编译的代码是属于类库,为什么还提示需要 Main 方法?且我已经明显的设置是类库了(OutputKind.DynamicallyLinkedLibrary),这个问题又该如何解决?

https://blog.csdn.net/tangyanzhi1111/article/details/84929369