1
votes

When reflecting into a unittests assembly (System.Reflection.Assembly.GetTypes()) from a console app I get a slew of file not found errors.

I tried adding a bunch of packages with minimal success, I'm unsure that is the way to go.

This is my project file.

    <Project Sdk="Microsoft.NET.Sdk">

      <PropertyGroup>
        <TargetFramework>netcoreapp2.2</TargetFramework>

        <IsPackable>false</IsPackable>   
      </PropertyGroup>

      <ItemGroup>
        <PackageReference Include="FluentAssertions" Version="5.9.0" />
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
        <PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
        <PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
        <PackageReference Include="PossumLabs.Specflow.Core" Version="1.0.0-CI00024" />
        <PackageReference Include="PossumLabs.Specflow.Selenium" Version="1.0.0-CI00030" />
        <PackageReference Include="Selenium.Chrome.WebDriver" Version="74.0.0" />
        <PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
        <PackageReference Include="SpecFlow" Version="3.0.225" />
        <PackageReference Include="SpecFlow.MsTest" Version="3.0.225" />
        <PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.0.225" />
        <PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.1" />
     </ItemGroup>

    </Project>

Could not load file or assembly 'Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.

Same goes for

Microsoft.VisualStudio.TestPlatform.TestFramework  

Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices Microsoft.VisualStudio.TestPlatform.ObjectModel Microsoft.VisualStudio.TestPlatform.TestFramework

I want to get these to copy to my output folder so that I can reflect into the unit test DLL.

1
publishing vs building helped, it is not only missing Microsoft.VisualStudio.TestPlatform.ObjectModel - Bas Hamer
ok, the problem was that the reflection code did not target the assembly narrowly enough. This error came from reflection into a microsoft dll in the same folder; that triggered the load problem. - Bas Hamer

1 Answers

0
votes

No Answer, just workaround;

This happened when reflecting into a Microsoft DLL; it seems that in .net core reflection changed a little bit and just blindly reflecting over every dll in a folder is no longer safe. Filtering the Dll by their dependency was possible, and that if how I narrow the scope; But even though I load every DLL the GetTypes will throw an error as mentioned above.

How you filter is less important than that you filter down what DLL's you look into.

Both start the same

            foreach (string dll in Directory.GetFiles(path, "*.dll"))
            assemblies.Add(Assembly.LoadFile(dll));

Bad code:

foreach (var assembly in assemblies)
        {}

Good Code:

foreach (var assembly in assemblies.Where(a=>a.GetReferencedAssemblies().Any(x=>x.Name.Contains("YourCompanyNamespace"))))
        {}