Create Func<Type> from TypeInfo and MethodInfo

I'm curious whether it's possible to create a delegate while having only the type at hand. Something like this:

var concreteType = DiscoverTypeInRuntime();
var methodName = "SomeNameIKnowInAdvance";
var methodInfo = concreteType.GetMethodInfo(methodName);
var dynamicallyConstructedFunc = DynamicallyConstructFunc(methodInfo)

The delegate should be of type

Func<ConcreteType>

UPDATED:

The exact type is not known in advance. It is discovered during the program run time. The delegate can't be of type Func<object only the Func<ConcreteType> is allowed. The method whose name is known is the factory method which returns an instance of ConcreteType thus the requirement.

UPDATED 2:

I will provide here some code to explain the use case. Hope this will clarify the question:

public class LoginProviderBuilder : FakeBuilderBase<ILoginProvider>
{        
    private readonly Dictionary<string, string> _users = new Dictionary<string, string>();

    private LoginProviderBuilder()
    {

    }
    //...
}

public class Module : ProvidersModuleBase
{
    protected override void OnRegisterProviders(IIocContainerRegistrator iocContainer)
    {
        base.OnRegisterProviders(iocContainer);
        RegisterAllBuilders(iocContainer, LoginProviderBuilder.CreateBuilder);
        RegisterAllBuilders(iocContainer, WarehouseProviderBuilder.CreateBuilder);
        RegisterAllBuilders(iocContainer, EventsProviderBuilder.CreateBuilder);
    }
}

protected void RegisterAllBuilders<TProvider>(IIocContainerRegistrator iocContainerRegistrator, 
        Func<FakeBuilderBase<TProvider>> defaultBuilderCreationFunc) where TProvider : class
    {
        var builders = BuildersCollectionContext.GetBuilders<TProvider>().ToArray();
        if (builders.Length == 0)
        {
            RegistrationHelper.RegisterBuilder(iocContainerRegistrator, defaultBuilderCreationFunc());
        }
        else
        {
            foreach (var builder in builders)
            {
                RegistrationHelper.RegisterBuilder(iocContainerRegistrator, builder);
            }
        }
    }

In short the task in hand is to discover all eligible builder type dynamically and register them automagically while somehow preserving the concrete type and the existing generics API.

If this code is not clear you can find the sample solution here: https://github.com/LogoFX/Samples.Specifications

Jon Skeet
people
quotationmark

You can create such a delegate - but you'll only be able to refer to it as a Delegate because you don't know the actual type at compile-time. It requires creating the appropriate delegate type from Func<T> using MakeGenericType.

var concreteType = DiscoverTypeInRuntime();
var methodName = "SomeNameIKnowInAdvance";
var methodInfo = concreteType.GetMethodInfo(methodName);
var funcType = typeof(Func<>).MakeGenericType(concreteType);
var func = Delegate.CreateDelegate(funcType, methodInfo)

That will create the right kind of delegate, referring to the right method... but the compile-time type will still just be Delegate.

people

See more on this question at Stackoverflow