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

  • Home
  • SEARCH
  • 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 6531517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:53:14+00:00 2026-05-25T09:53:14+00:00

I have a problem with casting a generic class to the interface it is

  • 0

I have a problem with casting a generic class to the interface it is implementing.

My code is like this:

interface foo
{
    void foobar();
}

class bar: foo
{
    public void foobar()
    {
        throw new NotImplementedException();
    }
}

now I have my factory that creates instances of my classes by the interface, mostly a simple microkernel (service locator). I will simplify it here. Normally it will look up the implementing class from the configs and the factory take the type as T but that doesn’t matter for the problem I have.

public static class Factory
{


    public static Lazy<foo> CreateLazyInstance()
    {
        Lazy<foo> instance;


        Type type = typeof(bar);

        Type lazyType = typeof(Lazy<>);
        Type toContruct = lazyType.MakeGenericType(type);

        instance = (Lazy<foo>)Activator.CreateInstance(toContruct);

        return instance;
    }
}

If will fail at:

instance = (Lazy<foo>)Activator.CreateInstance(toContruct);

and claim with an InvalidCastException that it is not possible to cast the type Lazy<bar> to Lazy<foo>.

Is there any way to tell the CLR that this cast will work or to workaround this problem?

  • 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-25T09:53:15+00:00Added an answer on May 25, 2026 at 9:53 am

    No – Lazy<T> is invariant – so a Lazy<string> is not a Lazy<object> for example. (As pointed out in comments, it couldn’t be declared as covariant in T, as it’s a class, not an interface or delegate.)

    However, you can convert one to the other easily enough:

    static Lazy<TOutput> CreateLazyProxy<TInput, TOutput>
        (Lazy<TInput> input) where TInput : TOutput
    {
        return new Lazy<TOutput>(() => input.Value);
    }
    

    Also, Func<T> is covariant, so this will work too:

    static Lazy<TOutput> CreateLazy<TInput, TOutput>(Func<TInput> func)
        where TInput : TOutput
    {
        return new Lazy<TOutput>(func);
    }
    

    (Not that you particularly need a method for that – if you’ve got a Func<TInput>, just construct a Lazy<TOutput> directly.)

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

Sidebar

Related Questions

I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
This time I have problem with virtual fields. I have core class for my
Here's the code: public interface IValidator<T> { bool IsValid(T obj); } public class OrderValidator:
I am using this code to make a Simple Command: public class SimpleCommand :
I have problem implementing the operator!= in a set class deriving from an abstact
I have a casting problem. I am writing a code to open all the
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
I've defined the following generic class public class ManagedClass<T> where T : ManagedClass<T> {
I have got a problem on casting an object to one of it's base
I've asked another related question to this here: casting dynamic to static problem I've

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.