4

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?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
Mark Heath
  • 48,273
  • 29
  • 137
  • 194

3 Answers3

3

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?

Community
  • 1
  • 1
Scott Ivey
  • 40,768
  • 21
  • 80
  • 118
  • Indeed, replace `List` with `Func` and you're done... – Marc Gravell Jun 25 '09 at 22:05
  • yes, looks like this will do it for me. thanks Now I have a problem that the compiler won't let me cast from () => Resolve(theArgumentType) back to the original T (even if I try to cast to object first). "Cannot convert lambda expression to type 'T' because it is not a delegate type" – Mark Heath Jun 25 '09 at 22:13
1

I misunderstood your question.

It is impossible to do it in one function the way you're trying to because the compiler must have a delegate type to create the lambda as at compile time.

However, this should work.

public T Resolve<T>()
{
    return (T)Resolve(typeof(T));
}

public Func<T> LazyResolve<T>()
{
    return () => Resolve<T>();
}
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • ok I think I understand why its not possible. Shame, because it would be a nice feature for an IoC container. – Mark Heath Jun 26 '09 at 15:16
0

In answer to the question in the comment, you need to invoke the lambda expression, not cast it.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964