Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8148729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:42:12+00:00 2026-06-06T14:42:12+00:00

I’m working on an application using MEF (specifically, MEF 2 Preview 5 ), and

  • 0

I’m working on an application using MEF (specifically, MEF 2 Preview 5), and I’ve run into a problem trying to import based on a generic interfaces.

I have an interface:

public interface IMessageHandler<in T>
{
    void HandleMessage(T message);
}

where T is a type of message to be handled. I’m importing these things into a catalog using the RegistrationBuilder:

RegistrationBuilder context = new RegistrationBuilder();

context.ForTypesDerivedFrom(typeof(IMessageHandler<>))
    .Export(builder => builder.AsContractType(typeof(IMessageHandler<>)));

and then, in the consuming class I’m using [ImportMany] to import a list of these into an IEnumerable<Lazy>>:

[ImportMany(typeof(IMessageHandler<>))]
IEnumerable<Lazy<IMessageHandler<object>, HandledMessageTypeAttribute>> _messageHandlers;

Now, here lies the first problem – you’re forced to assign a type to the generic interface at this point. I’m using Lazy<T, TMetadata> as the IMessageHandler<T> implementations have relevant metadata I want to consume (HandledMessageTypeAttribute).

Now, when I want to access any element in the IEnumerable<Lazy<>> collection I get the following exception:

Cannot cast the underlying exported value of type 
'MessageHandlerImplementation (ContractName="IMessageHandler(System.Object)")' 
to type 'IMessageHandler`1[System.Object]'.

I understand (roughly) why I’m getting the exception, the problem is I have no idea how to get around it. So, basically what I want to do is:

  1. Have a bunch of classes which implement an IMessageHandler<T> interface.
  2. Have them discovered at runtime with MEF.
  3. Import them into a collection which allows me to consume any metadata they have.
  4. Be able to instantiate them.

I understand I could simply make IMessageHandler non-generic and have IMessageHandler.HandleMessage() accept a parameter of type object but I was looking for a slightly more elegant solution

Any pointers or guidance is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:42:13+00:00Added an answer on June 6, 2026 at 2:42 pm

    I don’t see a much better way to do what you’re trying to achieve without using a non-generic interface. The root of the problem is the interface’s definition:

    public interface IMessageHandler<in T>
    

    which means that if we have two classes, A and B, where B derives from A, then this is allowed

    IMessageHandler<B> handler = new AHandler();
    

    but this is not:

    IMessageHandler<A> handler = new BHandler();
    

    You’re essentially trying to perform the latter, which is what causes the exception to be thrown. I assume that what you’d like to do is to be able to get a handler, given a type. If that’s the case then you should probably use a non-generic interface and have the message type available in the export metadata. Then you’ll have something like this:

    public IMessageHandler GetHandler<T>()
    {
        Type handlerType = typeof(T);
        return _messageHandlers.FirstOrDefault(x => x.Metadata.MessageType == handlerType);
    }
    

    You may find this question relevant as well. Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.