Cast generic class to interface

2011-09-06T00:16:22

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?

Copyright License:
Author:「Kolja」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/7310530/cast-generic-class-to-interface

About “Cast generic class to interface” questions

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() { ...
I am new user of interface. My problem is i have create a Generic class which have two parameter one is object type another is interface type. Now within class i can cast object using Reflection b...
I want to be able to cast a class with a generic type (which is another interface with a generic type) to it's interface but an exception is thrown. I can cast an object with a generic type to its
Is is possible to make the following compile without: Making IFooCollection generic Explicitly implementing IFooCollection.Items on FooCollection and performing an explicit cast. public int...
I have an external function which has following syntax: public void Set&lt;FieldType&gt;( Field field, FieldType value ) Field type can be some of primitive types like int, double, bool etc....
How can I cast a generic class with generic interface by passing Type that is determined at runtime? public class SomeDataChanged { public string EventName { get; set; } } public ...
I'm looking for a way to cast an object into a simple interface and to a generic interface which derives from the simple one. Here is my example: interface INonGeneric { } interface IGeneric&lt...
How can you cast an interface with a generic type to a common interface? Lets say we have the following interfaces/objects: public interface IAction : IAction&lt;object&gt; { } public interface I...
Is it possible to cast to a generic interface type where you don't know the actual generic type but DO know that the type is derived from some shared interface? I have a generic interface: public
I'm getting an unchecked cast warning when I use a Comparable interface with a generic or parametized List. I'm able to do the comparison using an ObjectList but not with a generic list. Here's my

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.