Fix bug when using a ctor with interfaces in MEF with unbound generic… #10665
Conversation
Hi @TMiNus, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution! TTYL, DNFBOT; |
@TMiNus, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR. |
This System.IO.DirectoryNotFoundException in the Windows_NT Release Build is not related with the changes at all O.o, prob. something going foobar in the CI server |
cc: @dsplaisted |
@dsplaisted @davkean can you please review the change? |
I'm not super familiar with MEF 2 but it looks like this change is modifying this code that previously looked for a public non static default constructor to look for that or a constructor that only takes Interfaces. My question would be, why only interfaces? Can't MEF also create parts that aren't interfaces? /cc @nblumhardt |
@ericstj that's correct, MEF2 can create/consume non-interface contracts. @TMiNus I just checked out your bug report linked above, I think the missing piece of information is that when no You can make your example work by adding the conventions.ForType(typeof(Logger<>))
.SelectConstructor(ci => ci.Single())
.Export(); Hope this helps, |
@nblumhardt I already knew about the |
@ericstj @nblumhardt The bug manifest when you don't have the mark. MEF2 requires the mark if you don't have a parameter-less constructor, which is completely wrong because you could have a constructor with only interfaces. With the change MEF2 will accept the constructor with interfaces as a good one and it will try to resolve the contract for those interfaces |
Hi @TMiNus - what I mean is, there's no bug here, it's by design. If you want MEF to choose constructors using a strategy different from the built-in one, you can use the Instead of calling conventions.ForType(typeof(Logger<>))
.SelectConstructorAcceptingInterfaces()
.Export(); Where static class PartConventionBuilderExtensions
{
public static PartConventionBuilder SelectConstructorAcceptingInterfaces<T>(this PartConventionBuilder @this)
{
return @this.SelectConstructor(constructors =>
constructors.Single(ci => ci.GetParameters().All(pi => pi.ParameterType.IsInterface)));
}
} |
@nblumhardt Did you read the stackoverflow link that I put in my comment, it seems like you didn´t. |
Just to be 200% sure I already tried your code and as I said before is a no go. As you can read in the title of the bug report, this is all related to unbound generics. I want MEF2 to close the generic for me. And if you see the Test that I enhanced, it clearly states that the export should be done as an unbound generic so your code will change to something like this:
And the realization of all this should be something like this:
And as stated in the stackoverflow link |
Ah I see. The problem then is that This PR is not the solution, unfortunately, but the bug report is valid. I'll add some notes over there in https://github.com/dotnet/corefx/issues/10627. Cheers! |
Well you can argue that it is a matter of perspective, if the MEF2 guys consider that As I said it is a matter of perspective and what the MEF2 team decide. |
@TMiNus @nblumhardt , sounds like we're going to look at making a different change for this based on this conversation and what I see here: #10627 . Should we close this PR? |
@Petermarcu The change that I talk about in my last comment in #10627 would cover a more wide scenario. This PR will fix the problem if you have a constructor with only interfaces |
ok, @nblumhardt what do you want to do with this PR? |
@Petermarcu if the |
ok, sounds good. I'm going to close this one and we can bring it back if there is still a gap after #10627 . |
…s #10627