Search code examples
unity-game-enginehololensmrtk

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


I am trying to port my project over from Unity 2017 LTS to 2018 LTS and MRTK2. Everything was going pretty smoothly until I tried to run the project on an HoloLens 1 emulator.

I get the following error:

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)

Searching for this error gave me nothing, I have not changed the project that Unity builds. I am running it in Debug and for x86. I am building through the normal Unity Build window.

Using Minimum Platform Version 10.0.17134.0 and Target SDK Version 10.0.18362.0

This also happens with a new empty Unity project, building an example will also give this error.


Solution

  • Realizing this may not just have been me, I submitted an issue in the MRTK github. It turns out this is a bug in Visual Studio, and they are working on resolving it with Microsoft.

    There's two ways to solve it, either target Windows version 15063 as the min version or change the yourpoject.csproj.

    If you don't want to change the min version these are the steps to take:

    1. Open yourproject.csproj in a texteditor

    2. Find the line <Target Name="BeforeResolveReferences" Condition="'$(BuildingProject)' == 'true'">

    3. Replace with:

      <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. Find the line <Message Importance="high" Text="Running AssemblyConverter..." />

    5. Replace with:

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

    After doing this, my project finally compiled and I was able to run it in the HoloLens 2 Emulator.