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 6230963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:50:50+00:00 2026-05-24T09:50:50+00:00

Im not sure how to state this question, because im not sure where the

  • 0

Im not sure how to state this question, because im not sure where the problem lies. I think its a genetric covariance issue, but the solution might be found somewhere else, maybe in the way the interfaces are designed, or in how the implementations are registered.

Anyway, the sample is trying to register all types implementing a generic interface, then later resolve the type using the type of the generic. Then trying to cast this type to its base type to be able to call a method on that implementation.

Its now failing when trying to cast. As an example, the first line of code fails to compile. When removed, the program fails on the line trying to cast the implementation.

class Program
{
    private static IContainer _container;

    static void Main(string[] args)
    {
        // Is this the problem?
        IHandler<IConfiguration> test = new MyHandler();

        // setup ioc
        var builder = new ContainerBuilder();
        builder.RegisterAssemblyTypes(typeof(Program).Assembly)
            //.Where(t => typeof(IHandler<IConfiguration>).IsAssignableFrom(t));
            .Where(t => t.GetInterfaces()
                .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IHandler<>))
                .Any()
                )
            .As(t => t.GetInterfaces()
                .Where(i => i.GetGenericTypeDefinition() == typeof(IHandler<>))
                .Single()
            );
        _container = builder.Build();

        // get my configuration impl
        var configuration = new MyConfiguration();

        // resolve handler for the configuration
        var configurationType = configuration.GetType();
        var handlerGenericType = typeof(IHandler<>);
        var handlerType = handlerGenericType.MakeGenericType(configurationType);
        var handler = _container.Resolve(handlerType);
        var typedHandler = (IHandler<IConfiguration>) handler;

        // handle it!
        typedHandler.Handle(configuration);
    }
}

public interface IConfiguration
{
}

public interface IHandler<T> where T : IConfiguration
{
    void Handle(T myConfiguration);
}

public class MyConfiguration : IConfiguration
{
}

public class MyHandler : IHandler<MyConfiguration>
{
    public void Handle(MyConfiguration myConfiguration)
    {
        Console.WriteLine("Handling my stuff...");
    }
}
  • 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-05-24T09:50:51+00:00Added an answer on May 24, 2026 at 9:50 am

    There is no way this is going to work.
    Here is the reason why:
    An IHandler<IConfiguration> requires an IConfiguration as parameter to Handle.
    If your first line would be valid, the following would compile:

    MyOtherConfiguration config = new MyOtherConfiguration();
    IHandler<IConfiguration> test = new MyHandler();
    test.Handle(config);
    

    Obviously, this is not correct, because MyHandler.Handle wants a MyConfiguration or derived type.

    Using contra-variance as described in my earlier version of this answer would allow you to do the following:

    IHandler<MyDerivedConfiguration> test = new MyHandler();
    

    with MyDerivedConfiguration being derived from MyConfiguration.

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

Sidebar

Related Questions

I think the title for this question is probably wrong, but I'm not sure
Not sure if this is a SO, SuperUser or ServerFault question, but I just
I think I may have overlooked something here, but I'm not sure why this
Not sure if I've just missed something but this doesn't work: $(this).children('td.threadtitle a').html('thread title');
I'm not sure if this is a duplicate but if it is please feel
Weird question, but I'm not sure if it's anti-pattern or not. Say I have
I'm quite ashemed to ask this question here because I'm sure that I'm missing
This question could be subjective. I'm not sure if it belongs here or on
I'm sorry this question is going to be a bit vague because I'm not
I'm not sure that I am asking this question properly... I assumed that I

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.