vendredi 20 mai 2016

C# How to regist lambda expression into Func<>

there is Register function below

private Dictionary<string, Func<ResolveContainer, object>> _allocFunctionList = ...;

public void Register<T>(Func<ResolveContainer, object> allocFunction)
{
    string allocFunctionKey = typeof(T).ToString();
    if (_allocFunctionList.ContainsKey(allocFunctionKey))
        throw new AllocFunctionKeyExistsException();

    _allocFunctionList[allocFunctionKey] = allocFunction;
}

and use case

Register<IFoo>(resolver => new Foo());
Register<IBar>(resolver => new Bar(resolver.Get<IFoo>()));

The Lambda Expression have many kind return type.

So declared return type is object in

Func<ResolveContainer, object>

private Dictionary<string, Func<ResolveContainer, object>> _allocFunctionList = ...;

Question 1

I need to use reflection with the object in the Register Function but The Register function knows Foo's interface(IFoo) and Func<..., object> -> return type

I want to know Foo and Bar's Type in The Register Function. how to know Lamda Expression's REAL return class type without update method definition.

Extract real return type in Lambda Expression with any Lib? or parse Lambda Expression?





Aucun commentaire:

Enregistrer un commentaire