Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a type is a Func<T> and get the type of the T [duplicate]

I am wondering whether I can upgrade a basic IoC container I am using to support lazy load. So if I have registered IFoo, I would like the IoC container to know how to fulfil both of the following dependencies (the first being the standard way IoC containers work, while the second returns a simple delegate that calls into the container for an IFoo when it is invoked).

public Bar(IFoo x)

public Bar2(Func<IFoo> lazyFoo)

The problem comes when I try to write the code that will actually do this. Is there a syntax that will make the following pseudo-code compile?

public T Resolve<T>()
{
    if (T is Func<X>)
        return (T) () => Resolve(typeof(X));
    return (T)Resolve(typeof(T));
}

Or to put my question another way, if I have a type T, how can I detect if it is an instance of Func<X>, and if so, what is the type of X?

like image 416
Mark Heath Avatar asked Oct 20 '25 05:10

Mark Heath


1 Answers

take a look at this question from this morning - might give you a good start - C# generic list <T> how to get the type of T?

like image 99
Scott Ivey Avatar answered Oct 22 '25 18:10

Scott Ivey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!