The "GetReferenceNearestTargetFrameworkTask" task has been declared or used incorrectly

时间:2017-12-18 07:37:28

标签: visual-studio msbuild

The error above happens with VS 2017 15.5.2 in a test project while executing the following test. There are binding redirects in the test.vbproj file but nothing for msbuild.

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ if (indexPath.row==0)
{
    CustomCellA *aCell = [tableView dequeueReusableCellWithIdentifier:@"ACell"];
    return aCell;
} else if(indexPath.row==1)
{
    CustomCellB *bCell = [tableView dequeueReusableCellWithIdentifier:@"BCell"];
    return bCell;
}else
{
    CustomCellC *cCell = [tableView dequeueReusableCellWithIdentifier:@"CCell"];
    return cCell;
} 
}
-(void)insertNewACell:(UIButton *)addButton atIndexPath:(NSIndexPath *)addBtnIndexPath
{
newArray = [[NSMutableArray alloc]init];
[newArray addObject:@"XXXX"];
[newArray addObject:@"YYYY"];
[newArray addObject:@"YYYY"];
[sectionRowItems addObject:newArray];
[sectionRowItems insertObject:newArray atIndex:addBtnIndexPath.row];
[self.numericTable reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return alphabetArray.count
}

The whole message is

[Failure] Msbuild failed when processing the file 'C:\Repos\roslyn-master\src\Samples\CSharp\APISampleUnitTests\APISampleUnitTestsCS.csproj' with message: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets: (1601, 5): The "GetReferenceNearestTargetFrameworkTask" task could not be instantiated from the assembly "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.Build.Tasks.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'NuGet.Build.Tasks.GetReferenceNearestTargetFrameworkTask' to type 'Microsoft.Build.Framework.ITask'. C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets: (1601, 5): The "GetReferenceNearestTargetFrameworkTask" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.

1 个答案:

答案 0 :(得分:0)

如果代码超过1个,则此代码将允许选择VS实例

Imports Microsoft.Build.Locator

Public Class VS_Selector_Dialog1
    Private m_instance As VisualStudioInstance = Nothing
    Private SelectedRow As Integer = -1
    Private visualStudioInstances() As VisualStudioInstance = MSBuildLocator.QueryVisualStudioInstances().ToArray()
    Public ReadOnly Property MSBuildInstance As VisualStudioInstance
        Get
            Return m_instance
        End Get
    End Property

    Private Sub Cancel_Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Cancel_Button.Click
        DialogResult = DialogResult.Cancel
        Close()
    End Sub

    Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
        m_instance = visualStudioInstances(DataGridView1.CurrentRow.Index)
    End Sub

    Private Sub OK_Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OK_Button.Click
        If m_instance Is Nothing Then
            Exit Sub
        End If
        DialogResult = DialogResult.OK
        Close()
    End Sub

    Private Sub VS_Selector_Dialog1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim BestIndex As Integer = -1
        Text = "Multiple installs of MSBuild detected please select one:"
        Dim InstanceTable As New DataTable
        InstanceTable.Columns.Add("InstanceNumber")
        InstanceTable.Columns.Add("Edition")
        InstanceTable.Columns.Add("Version")
        InstanceTable.Columns.Add("MSBuildPath")
        For Index As Integer = 0 To visualStudioInstances.Length - 1
            If visualStudioInstances(Index).Name.Contains(" Build ") Then
                Continue For
            End If
            BestIndex = Index
            Dim rowToAdd As DataRow = InstanceTable.NewRow()
            rowToAdd("InstanceNumber") = Index + 1
            rowToAdd("Edition") = visualStudioInstances(Index).Name
            rowToAdd("Version") = visualStudioInstances(Index).Version
            rowToAdd("MSBuildPath") = visualStudioInstances(Index).MSBuildPath
            InstanceTable.Rows.Add(rowToAdd)
        Next
        DataGridView1.DataSource = InstanceTable
        If InstanceTable.Rows.Count = 1 Then
            m_instance = visualStudioInstances(BestIndex)
            DialogResult = DialogResult.OK
            Close()
        End If
     End Sub
End Class

让用户选择实例,或者选择“最佳”实例

If VS_Selector_Dialog1.ShowDialog(Me) <> DialogResult.OK Then
    Stop
End If

一旦您拥有正确的实例,MSBuild将正常工作

Console.WriteLine($"Using MSBuild at '{VS_Selector_Dialog1.MSBuildInstance.MSBuildPath}' to load projects.")
' NOTE: Be sure to register an instance with the MSBuildLocator
'       before calling MSBuildWorkspace.Create()
'       otherwise, MSBuildWorkspace won't MEF compose.
MSBuildLocator.RegisterInstance(VS_Selector_Dialog1.MSBuildInstance)
Using Workspace As MSBuildWorkspace = MSBuildWorkspace.Create()
    AddHandler Workspace.WorkspaceFailed, AddressOf MSBuildWorkspaceFailed
    Dim currentProject As Project = Workspace.OpenProjectAsync(.FileName).Result
    Workspace.LoadMetadataForReferencedProjects = True
    If currentProject.HasDocuments Then
        For Each document As Document In currentProject.Documents