在Windows 10 Universal Windows库中引用BindingFlags类型时无法加载程序集

时间:2022-09-03 22:31:07

When running unit tests, calling any method in my portable runtime library DLL that references the "System.Reflection.TypeExtensions" generates a FileNotFound exception, searching for "System.Reflection.TypeExtensions". The error does not occur when the same code is executed in a Windows 10 Universal app.

运行单元测试时,调用我的可移植运行库DLL中引用“System.Reflection.TypeExtensions”的任何方法都会生成FileNotFound异常,搜索“System.Reflection.TypeExtensions”。在Windows 10 Universal应用程序中执行相同的代码时不会发生此错误。

The project is a C# portable runtime library, configured to support .net Framework 4.6 and Windows Universal 10.0. The Test project is configured to use .net Framework 4.6.

该项目是一个C#可移植运行时库,配置为支持.net Framework 4.6和Windows Universal 10.0。 Test项目配置为使用.net Framework 4.6。

Whenever I attempt to call a method that uses the System.Reflection.BindingFlags type, I get the following exception. The exception occurs as the call starts (presumably while jit-ing the function).

每当我尝试调用使用System.Reflection.BindingFlags类型的方法时,我都会收到以下异常。调用开始时会发生异常(大概是在启动函数时)。

Test method Sfx.Test.SignalExpressionTest.TestAddExpressions threw exception: 
System.IO.FileNotFoundException: Could not load file or assembly 'System.Reflection.TypeExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.=== Pre-bind state information ===
LOG: DisplayName = System.Reflection.TypeExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)

2 个解决方案

#1


3  

Adding those packages is the right thing to do. Here is why you see this behavior:

添加这些包是正确的做法。这就是为什么你看到这种行为:

  1. BindingFlags is currently [1] exposed in System.Reflection.TypeExtensions.
  2. BindingFlags目前[1]在System.Reflection.TypeExtensions中公开。
  3. When you compile a .NET Core class library, the compiler records the type reference as System.Reflection.TypeExtensions!System.Reflection.BindingFlags.
  4. 编译.NET Core类库时,编译器将类型引用记录为System.Reflection.TypeExtensions!System.Reflection.BindingFlags。
  5. When you consume the class library from a .NET Framework application, you need to add a reference to System.Reflection.TypeExtensions otherwise the compiler cannot resolve the type. The same is true for a unit testing project which has to deploy all the code in order to run.
  6. 从.NET Framework应用程序使用类库时,需要添加对System.Reflection.TypeExtensions的引用,否则编译器无法解析该类型。对于必须部署所有代码才能运行的单元测试项目也是如此。
  7. At runtime, the CLR will resolve the type, find a type forward that points to mscorlib!System.Reflection.BindingFlags and happily run your code.
  8. 在运行时,CLR将解析类型,找到指向mscorlib的类型转发!System.Reflection.BindingFlags并愉快地运行您的代码。

As a general rule of thumb: .NET Core is designed to deploy the framework in an app-local fashion, in other words, when your application is deployed to a folder, the closure of all your dependencies need to be deployed too. Although unit test projects are conceptually class libraries, the same applies because they are executed like applications.

作为一般经验法则:.NET Core旨在以应用程序本地方式部署框架,换句话说,当您的应用程序部署到文件夹时,还需要部署所有依赖项的关闭。虽然单元测试项目是概念上的类库,但同样适用,因为它们像应用程序一样执行。

Of course, we don't expect folks to manually tweak the references and hunt down dependencies. What you currently see are pre-release bits where not all pieces in our tooling experiences do the right thing.

当然,我们不希望人们手动调整引用并追捕依赖关系。您目前看到的是预发布位,并非我们的工具体验中的所有部分都做正确的事情。

Two questions from my side:

我这两个问题:

  1. Which unit testing framework do you use? I assume it's MSTest (Microsoft.VisualStudio.TestTools.UnitTesting), as opposed to xUnit or NUnit?

    您使用哪个单元测试框架?我假设它是MSTest(Microsoft.VisualStudio.TestTools.UnitTesting),而不是xUnit或NUnit?

  2. Which runner are you using? I assume it's the built-in Visual Studio Test Explorer (as opposed to ReSharper or TestDriven.NET)?

    你在用哪个跑步者?我假设它是内置的Visual Studio Test Explorer(与ReSharper或TestDriven.NET相对)?


[1] I say currently because based on customer feedback we decided to move it back into System.Reflection, together with the APIs that take BindingFlags.

[1]我之前说过,因为根据客户反馈,我们决定将其与带有BindingFlags的API一起移回System.Reflection。

#2


4  

add reference to nuget packages:

添加对nuget包的引用:

System.Reflection
System.Reflection.Extensions
System.Reflection.Primitives

#1


3  

Adding those packages is the right thing to do. Here is why you see this behavior:

添加这些包是正确的做法。这就是为什么你看到这种行为:

  1. BindingFlags is currently [1] exposed in System.Reflection.TypeExtensions.
  2. BindingFlags目前[1]在System.Reflection.TypeExtensions中公开。
  3. When you compile a .NET Core class library, the compiler records the type reference as System.Reflection.TypeExtensions!System.Reflection.BindingFlags.
  4. 编译.NET Core类库时,编译器将类型引用记录为System.Reflection.TypeExtensions!System.Reflection.BindingFlags。
  5. When you consume the class library from a .NET Framework application, you need to add a reference to System.Reflection.TypeExtensions otherwise the compiler cannot resolve the type. The same is true for a unit testing project which has to deploy all the code in order to run.
  6. 从.NET Framework应用程序使用类库时,需要添加对System.Reflection.TypeExtensions的引用,否则编译器无法解析该类型。对于必须部署所有代码才能运行的单元测试项目也是如此。
  7. At runtime, the CLR will resolve the type, find a type forward that points to mscorlib!System.Reflection.BindingFlags and happily run your code.
  8. 在运行时,CLR将解析类型,找到指向mscorlib的类型转发!System.Reflection.BindingFlags并愉快地运行您的代码。

As a general rule of thumb: .NET Core is designed to deploy the framework in an app-local fashion, in other words, when your application is deployed to a folder, the closure of all your dependencies need to be deployed too. Although unit test projects are conceptually class libraries, the same applies because they are executed like applications.

作为一般经验法则:.NET Core旨在以应用程序本地方式部署框架,换句话说,当您的应用程序部署到文件夹时,还需要部署所有依赖项的关闭。虽然单元测试项目是概念上的类库,但同样适用,因为它们像应用程序一样执行。

Of course, we don't expect folks to manually tweak the references and hunt down dependencies. What you currently see are pre-release bits where not all pieces in our tooling experiences do the right thing.

当然,我们不希望人们手动调整引用并追捕依赖关系。您目前看到的是预发布位,并非我们的工具体验中的所有部分都做正确的事情。

Two questions from my side:

我这两个问题:

  1. Which unit testing framework do you use? I assume it's MSTest (Microsoft.VisualStudio.TestTools.UnitTesting), as opposed to xUnit or NUnit?

    您使用哪个单元测试框架?我假设它是MSTest(Microsoft.VisualStudio.TestTools.UnitTesting),而不是xUnit或NUnit?

  2. Which runner are you using? I assume it's the built-in Visual Studio Test Explorer (as opposed to ReSharper or TestDriven.NET)?

    你在用哪个跑步者?我假设它是内置的Visual Studio Test Explorer(与ReSharper或TestDriven.NET相对)?


[1] I say currently because based on customer feedback we decided to move it back into System.Reflection, together with the APIs that take BindingFlags.

[1]我之前说过,因为根据客户反馈,我们决定将其与带有BindingFlags的API一起移回System.Reflection。

#2


4  

add reference to nuget packages:

添加对nuget包的引用:

System.Reflection
System.Reflection.Extensions
System.Reflection.Primitives