vendredi 8 septembre 2017

Create generic delegate using reflection with late bound type

I have a method like this:

string[] mySqlTypeNames = new string[] { "MySql.Data.MySqlClient.MySqlConnection", "MySql.Data.MySqlClient.MySqlConnection" };
string[] mySqlAssemblies = new string[] { "MySql.Data", "MySqlConnector" };
Type mySqlConnection = ConnectorHelpers.FindType(mySqlAssemblies, mySqlTypeNames);
builder.Register(ctx =>
{
    var ctxLocal = ctx.Resolve<IComponentContext>();
    Func<string,MySqlConnection> selectorFactory = name => (MySqlConnection)ctxLocal.ResolveNamed(name, mySqlConnection);
    return selectorFactory;
});

How can I generate the same code as above as late bound? I don't want compile time dependency on MySqlConnection class - I want it found and used through reflection.

I have a feeling this can only be done using expression trees, but I got completely lost trying to do it.





Aucun commentaire:

Enregistrer un commentaire