Roslyn笔记

.Net Core项目中引用 Console 类出现错误:CS0103

原因是因为在.Net Core中, Console 类放在了 System.Console.dll 中,一般教程是从object类的元数据(即System.dll)中调用的:

MetadataReference[] references = new MetadataReference[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
}

获得当前AppDomain所有引用的Assembly组件的方法:

 var assemblies = AppDomain.CurrentDomain.GetAssemblies()
  .Where(asm => !asm.IsDynamic && string.IsNullOrWhiteSpace(asm.Location))
  .Select(asm => MetadataReference.CreateFromFile(asm.Location))
  .Cast<MetadataReference>()
  .ToArray();
var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
references.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Private.CoreLib.dll")));
  references.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Console.dll")));
  references.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll")));

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

4 + 11 =