我正在开发一个项目,我们正在使用Roslyn为我们编译一些模板.现在,当我正在编译模板时,我收到了多个错误CompileResult.Diagnostics.
错误是:
(21,6): error CS0012: The type 'System.Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
(21,6): error CS0012: The type 'System.Type' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Run Code Online (Sandbox Code Playgroud)
看到这些错误,我认为我没有System.Runtime正确添加对程序集的引用.但是,在检查加载的组件后,这似乎是有序的.
private IEnumerable<MetadataReference> GetGlobalReferences()
{
var assemblies = new []
{
typeof(System.Object).Assembly, //mscorlib
typeof(System.Composition.ExportAttribute).Assembly, //System.Composition (MEF)
typeof(System.CodeDom.Compiler.CodeCompiler).Assembly, //System.CodeDom.Compiler
};
var refs = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,我使用Roslyn从一个解决方案编译项目.
foreach (var projectId in solution.GetProjectDependencyGraph().GetTopologicallySortedProjects())
{
var project = solution.GetProject(projectId);
var compilation = project.GetCompilationAsync().Result;
var errors = compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error);
// ...
Run Code Online (Sandbox Code Playgroud)
编译包含诸如的错误
错误CS0012:类型"任务"在未引用的程序集中定义.您必须添加对程序集'System.Threading.Tasks,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'的引用.
为什么Roslyn不接受对mscorlib的现有引用?
有CompilationOptions什么我应该考虑的吗?根据这个线程,我试过,assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default但它没有帮助.我试图与之合作,metadataReferenceResolver但找不到有关它的更多信息.
在Roslyn中的解决方案之后没有引用System.Runtime我实现了代码,确保项目引用了mscorlib.dll,System.Core.dll,System.dll和System.Runtime.dll,这样我的项目和编译就有了参考文献:
附注:参考#7已添加此方式.该项目已经引用了#1,2和3,删除它们并替换为C:\ Windows\Microsoft.NET\Framework中的那些并没有解决问题.
project.MetadataReferences.ToList()
Count = 8
[0]: Assembly Path='C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll'
[1]: Assembly Path='C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll'
[2]: …Run Code Online (Sandbox Code Playgroud)