2015-02-08 33 views
6

Tôi đang tạo một công cụ và sử dụng Roslyn để tải tệp giải pháp và sau đó đến lấy phần biên dịch cho các dự án được chứa.Biên dịch không thành công với tệp sln được tải bằng Roslyn, nhưng biên dịch trong Visual Studio

Nhưng một số lỗi phát sinh (5000 +) ... đây là những vài trong số họ:

  • CS0518: Predefined gõ 'System.String' không được xác định hoặc nhập khẩu
  • CS0246: Loại hoặc namespace tên 'hệ thống' không thể được tìm thấy (là bạn thiếu một chỉ thị sử dụng hoặc tham khảo một hội?)

Các mã sau đây đã có hai cố gắng:

  • # 1 - cố gắng để biên dịch ngay lập tức - những gì tôi đã cố gắng trong một giây phút đầu tiên ... này đã có 5000+ lỗi
  • # 2 - biên soạn hệ thống thêm tham chiếu - những gì tôi đã cố gắng để làm cho nó hoạt , và nó đã làm việc ... nhưng không như mong đợi

mã:

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(); 
} 

phiên bản Roslyn đang được sử dụng: 1.0.0.0-beta2

Câu hỏi đặt ra là:

Kể từ khi tôi đã tải một tập tin giải pháp hợp lệ, lưu với Visual Studio 2012, nhắm mục tiêu Net Framework v4.5 và biên soạn với sự thành công trong IDE ... tại sao tôi phải thêm tham chiếu hệ thống để làm cho nó hoạt động?.

Tôi mong đợi nó đi kèm với các tài liệu tham khảo đã ... hệ thống tham chiếu đến đúng .Net Framework phiên bản. Điều đó có nghĩa là thậm chí # 2 sẽ không hoạt động luôn vì tôi phải định vị đúng các tệp .Net Framework phiên bản, thay vì sử dụng các công cụ tôi đang hiện đang biên dịch.

EDIT

Dưới đây là nội dung csproj file:

<?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> 
+0

Loại dự án nào? Lỗi gì? – SLaks

+0

C# dự án (csproj) ... tất cả các lỗi đều là lỗi tham chiếu ... Tôi đã chỉnh sửa câu hỏi và thêm một vài câu hỏi. –

+0

Cách tiếp cận số một * nên * hoạt động. Nếu bạn gỡ lỗi, các dự án có nhận được bất kỳ tham chiếu nào không? Có bất kỳ dự án Portable Class Libraries không? –

Trả lời

2

cuối cùng tôi đã tìm thấy một câu trả lời ... Tôi đã sử dụng .Net Framework 4.6 xem trước để làm công cụ.

Tôi đã gỡ cài đặt Visual Studio 2015, nhưng không phải khung 4.6.

Vì vậy, tôi đã sử dụng nhầm nó khi tạo dự án công cụ, bằng cách chọn khung .Net 4.5.3 trong Visual Studio 2013 (xuất hiện bên trong VS2013 sau khi cài đặt VS2015 CTP).

Bằng cách chuyển khung thành 4.5 nó hoạt động và tải chính xác MetadataReferences.

Các vấn đề liên quan