Skip to content

Instantly share code, notes, and snippets.

@miklund
Created September 19, 2013 06:54
Show Gist options
  • Save miklund/6619891 to your computer and use it in GitHub Desktop.
Save miklund/6619891 to your computer and use it in GitHub Desktop.
MEF Open Generics problem in unit test project. 1. This works if run in a console exe project 2. Does not work if you run through nunit-console(-x86).exe 3. Does not work if run through Resharper 4. Works! if run through Visual Studio test runner (!) Could someone smarter than me figure this out? Need to reference System.ComponentModel.Compositi…
namespace Test
{
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using NUnit.Framework;
[Export]
public class Class0
{
}
[Export]
public class Class1<T>
{
}
[TestFixture]
public class TestClass1
{
[Test]
public void ShouldResolve()
{
var typeCatalog1 = new TypeCatalog(typeof(Class0));
var typeParts1 = typeCatalog1.Parts.ToArray();
Assert.That(typeParts1.Count(), Is.GreaterThan(0), "Expected Class0 to be in TypeCatalog");
var typeCatalog2 = new TypeCatalog(typeof(Class1<>));
var typeParts2 = typeCatalog2.Parts.ToArray();
Assert.That(typeParts2.Count(), Is.GreaterThan(0), "Expected Class1 to be in TypeCatalog");
}
}
}
@dkataskin
Copy link

Have the same problem. Any luck on investigating why is it happening?

@Caldas
Copy link

Caldas commented May 29, 2014

Anyone has a clue on this?

@mathobbs
Copy link

mathobbs commented Mar 5, 2015

I ran into this too and also loading assemblies into PowerShell, the workaround was (presumably not needed with some future releases):

//Resharper test runner fix due to MEF open generic export fix requiring .NET 4.5.1 vs. some hard-coded Framework version check...
const string v45TargetFrameworkName = ".NETFramework,Version=v4.5";
var fusionStoreField = typeof(AppDomain).GetField("_FusionStore", System.Reflection.BindingFlags.NonPublic |System.Reflection.BindingFlags.Instance);
var fusionStore = (AppDomainSetup)(fusionStoreField.GetValue(AppDomain.CurrentDomain));
if (fusionStore.TargetFrameworkName != v45TargetFrameworkName)
    fusionStore.TargetFrameworkName = v45TargetFrameworkName;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment