gpt4 book ai didi

c# - 使用 Roslyn 加载 sln 文件时编译失败,但在 Visual Studio 中编译

转载 作者:太空狗 更新时间:2023-10-29 20:36:22 25 4
gpt4 key购买 nike

我正在制作一个工具,并使用 Roslyn 加载解决方案文件,然后获取包含项目的编译。

但是出现了一些错误(5000+)...这些只是其中的一小部分:

  • CS0518:未定义或导入预定义类型“System.String”
  • CS0246:找不到类型或命名空间名称“System”(是否缺少 using 指令或程序集引用?)

下面的代码有两次尝试:

  • #1 - 尝试立即编译 - 我在第一时间尝试的...这个有 5000 多个错误
  • #2 - 编译添加系统引用 - 我试图让它起作用,它确实起作用了......但没有达到预期

代码:

var workspace = MSBuildWorkspace.Create();
var solution = await workspace.OpenSolutionAsync(@"C:\Projects\MyLib\MyLib.sln");

var sortedProject = solution
.GetProjectDependencyGraph()
.GetTopologicallySortedProjects()
.Select(solution.GetProject);

foreach (var project in sortedProject)
{
//
// #1 - trying to compile right away
//
var compilation1 = await project.GetCompilationAsync();
var diag1 = compilation1.GetDiagnostics();

//
// #2 - compiling adding system references
//
var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
var project2 = project.AddMetadataReferences(
new[]
{
MetadataReference.CreateFromFile(
Path.Combine(assemblyPath, "mscorlib.dll")),
MetadataReference.CreateFromFile(
Path.Combine(assemblyPath, "System.dll")),
MetadataReference.CreateFromFile(
Path.Combine(assemblyPath, "System.Core.dll")),
});

var compilation2 = await project2.GetCompilationAsync();
var diag2 = compilation2.GetDiagnostics();
}

正在使用的 Roslyn 版本:1.0.0.0-beta2

问题是:

因为我已经加载了一个有效的解决方案文件,并使用 Visual Studio 2012 保存,以 .Net Framework v4.5 为目标并在 IDE 中成功编译...为什么我必须添加系统引用才能使其正常工作?

我预计它已经带有引用...系统引用正确.Net Framework 版本。这意味着即使 #2 也不会总是有效,因为我必须找到正确的 .Net Framework 版本文件,而不是使用我正在使用的工具目前正在编译。

编辑

csproj 文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{E8757858-B17B-43D4-AA13-412E552386DC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DataStructures</RootNamespace>
<AssemblyName>DataStructures</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug.net45\</OutputPath>
<DefineConstants>TRACE;DEBUG;net45</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release.net45\</OutputPath>
<DefineConstants>TRACE;net45</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release.net45\DataStructures.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>masb.public.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Continuous\ContinuousSet.cs" />
<Compile Include="Continuous\ContinuousSetEqualityComparer.cs" />
<Compile Include="Continuous\Interval.cs" />
<Compile Include="Continuous\ISetOperator.cs" />
<Compile Include="Continuous\PointState.cs" />
<Compile Include="Continuous\RedundancyType.cs" />
<Compile Include="EnumerableExtensions.cs" />
<Compile Include="DictionaryExtensions.cs" />
<Compile Include="IImmutable.cs" />
<Compile Include="IImmutablePrototype.cs" />
<Compile Include="Immutable\FuncCached.cs" />
<Compile Include="Immutable\IImmutableSpecificTreeBuilderWithContext.cs" />
<Compile Include="Immutable\IImmutableTreeBuilderWithContext.cs" />
<Compile Include="Immutable\IMayBeImmutable.cs" />
<Compile Include="Immutable\ImmutableSpecificTreeBuilderWithContext.cs" />
<Compile Include="Immutable\ImmutableTreeBuilderWithContext.cs" />
<Compile Include="Immutable\ImmutableTreeBuilderWithContextExtensions.cs" />
<Compile Include="Immutable\IReadableForest.cs" />
<Compile Include="Immutable\IImmutableSpecificTreeBuilder.cs" />
<Compile Include="Immutable\IImmutableTreeBuilder.cs" />
<Compile Include="Immutable\ImmutableCollection.cs" />
<Compile Include="Immutable\ImmutableForestExtensions.cs" />
<Compile Include="Immutable\ImmutableSpecificTreeBuilder.cs" />
<Compile Include="Immutable\ImmutableTreeBuilder.cs" />
<Compile Include="Immutable\ImmutableTreeBuilderExtensions.cs" />
<Compile Include="Immutable\TreeBuildingContextBase.cs" />
<Compile Include="Immutable\TreeBuildingContext.cs" />
<Compile Include="Immutable\Tree\Branch.cs" />
<Compile Include="Immutable\Tree\INodeFactory.cs" />
<Compile Include="Immutable\Tree\INonRoot.cs" />
<Compile Include="Immutable\Tree\Node.cs" />
<Compile Include="Immutable\ImmutableForest.cs" />
<Compile Include="Immutable\Tree\IBranch.cs" />
<Compile Include="Immutable\Tree\ILeaf.cs" />
<Compile Include="Immutable\Tree\INode.cs" />
<Compile Include="Immutable\Tree\IRoot.cs" />
<Compile Include="Immutable\Tree\Leaf.cs" />
<Compile Include="Immutable\Tree\NodeExtensions.cs" />
<Compile Include="Immutable\Tree\NonRoot.cs" />
<Compile Include="Immutable\Tree\Root.cs" />
<Compile Include="Immutable\Tree\RootBranch.cs" />
<Compile Include="Immutable\Tree\RootLeaf.cs" />
<Compile Include="Immutable\ImmutableTree.cs" />
<Compile Include="Immutable\Tree\Visitor.cs" />
<Compile Include="JetBrains.Annotations\JetBrains.Annotations.cs" />
<Compile Include="Monads\IOption.cs" />
<Compile Include="Monads\None.cs" />
<Compile Include="Monads\OptionExtensions.cs" />
<Compile Include="Monads\Some.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SystemExtensions\ObjectExtensions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="masb.public.snk" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

最佳答案

我终于找到了答案...我正在使用 .Net Framework 4.6 预览版来制作该工具。

我已经卸载了 Visual Studio 2015,但没有卸载框架 4.6。

所以我在创建工具项目时错误地使用了它,在Visual Studio 2013中选择.Net framework 4.5.3(安装VS2015 CTP后在VS2013中出现)。

通过将框架切换到 4.5,它可以正常工作并正确加载 MetadataReferences

关于c# - 使用 Roslyn 加载 sln 文件时编译失败,但在 Visual Studio 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28389794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com