运行 MRTK2 项目给出“解析失败 System.Reflection.BindingFlags”

Running MRTK2 project gives " Failed to resolve System.Reflection.BindingFlags"

我正在尝试将我的项目从 Unity 2017 LTS 移植到 2018 LTS 和 MRTK2。在我尝试 运行 在 HoloLens 1 模拟器上运行该项目之前,一切都进行得很顺利。

我收到以下错误:

1>  System.Exception: Failed to resolve System.Reflection.BindingFlags
1>     at Unity.ModuleContext.Retarget(TypeReference type, GenericContext context)
1>     at Unity.ModuleContext.Retarget(MethodReference method, GenericContext context)
1>     at Unity.FixReferencesStep.Visit(MethodDefinition method, GenericContext context)
1>     at Unity.FixReferencesStep.Visit(TypeDefinition type)
1>     at Unity.TypeDefinitionDispatcher.DispatchType(TypeDefinition type)
1>     at Unity.TypeDefinitionDispatcher.DispatchType(TypeDefinition type)
1>     at Unity.TypeDefinitionDispatcher..ctor(ModuleDefinition module, ITypeDefinitionVisitor visitor)
1>     at Unity.FixReferencesStep.ProcessModule()
1>     at Unity.ModuleStep.Execute()
1>     at Unity.FixReferencesStep.Execute()
1>     at Unity.Step.Execute(OperationContext operationContext, IStepContext previousStepContext)
1>     at Unity.Operation.Execute()
1>     at Unity.Program.Main(String[] args)

搜索此错误没有任何结果,我没有更改 Unity 构建的项目。我 运行 在 Debugx86 中使用它。我正在通过正常的 Unity Build window.

进行构建

使用 Minimum Platform Version 10.0.17134.0Target SDK Version 10.0.18362.0

新的空 Unity 项目也会出现这种情况,构建示例也会出现此错误。

我遇到了类似的问题,为了解决这个问题,我更改为 il2cpp 后端。

意识到这可能不仅仅是我,我提交了 an issue in the MRTK github。事实证明这是 Visual Studio 中的一个错误,他们正在与 Microsoft 一起解决它。

There's two ways to solve it,要么将 Windows 版本 15063 作为最低版本,要么更改 yourpoject.csproj.

如果您不想更改最低版本,请执行以下步骤:

  1. 在文本编辑器中打开你的项目.csproj

  2. 找到行<Target Name="BeforeResolveReferences" Condition="'$(BuildingProject)' == 'true'">

  3. 替换为:

    <UsingTask TaskName="FixProjectJson" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
        <ParameterGroup />
        <Task>
          <Using Namespace="System" />
          <Using Namespace="System.IO" />
          <Code Type="Fragment" Language="cs">
            <![CDATA[File.WriteAllText("project.lock.json", File.ReadAllText("project.lock.json").Replace("ref/netstandard1.5/System.Reflection.TypeExtensions.dll", "ref/netstandard1.3/System.Reflection.TypeExtensions.dll"));]]>
          </Code>
        </Task>
      </UsingTask>
      <Target Name="BeforeResolveReferences" Condition="'$(BuildingProject)' == 'true'">
    
  4. 找到行<Message Importance="high" Text="Running AssemblyConverter..." />

  5. 替换为:

    <Message Importance="high" Text="Running AssemblyConverter..." />
    <FixProjectJson />
    

这样做之后,我的项目终于编译好了,我可以在 HoloLens 2 模拟器中 运行 它了。