.net Core amd Roslyn CSharpCompilation, The type 'Object' is defined in an assembly that is not referenced

喜欢而已 提交于 2019-12-03 11:04:35
Gusman

Ok, got it finally working:

    string testClass = @"using System; 
    namespace test{

     public class tes
     {

       public string unescape(string Text)
      { 
        return Uri.UnescapeDataString(Text);
      } 

     }

    }";

    var dd = typeof(Enumerable).GetTypeInfo().Assembly.Location;
    var coreDir = Directory.GetParent(dd);

    var compilation = CSharpCompilation.Create(Guid.NewGuid().ToString() + ".dll")
        .WithOptions(new CSharpCompilationOptions(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary))
        .AddReferences(
        MetadataReference.CreateFromFile(typeof(Object).GetTypeInfo().Assembly.Location),
        MetadataReference.CreateFromFile(typeof(Uri).GetTypeInfo().Assembly.Location),
        MetadataReference.CreateFromFile(coreDir.FullName + Path.DirectorySeparatorChar + "mscorlib.dll"),
        MetadataReference.CreateFromFile(coreDir.FullName + Path.DirectorySeparatorChar + "System.Runtime.dll")
        )
        .AddSyntaxTrees(CSharpSyntaxTree.ParseText(testClass));

    var eResult = compilation.Emit("test.dll");

I don't like it because the hardcoding, but it seems it's the only way to get it work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!