user1948635
user1948635

Reputation: 1409

Error binding to target method

I am trying to call a static method from a helper class, of which the type is not known until runtime. I thought I had solved the problem however I am getting the following error -

"Error binding to target method."

Can anyone see what is wrong with this code? Any help would be appreciated..

    Delegate del = Delegate.CreateDelegate(typeof(Func<string>),
                    typeof(RepositoryStringExtensions).GetMethod("GetTableName", BindingFlags.Static | BindingFlags.Public)
                    .MakeGenericMethod(new Type[] { objectType })) as Func<string>;

    string tableName = (string)del.DynamicInvoke(context);

The type "objecttype" above can be any class, "RepositoryStringExtensions" is the helper class which contains the method, the method it is trying to call is displayed below -

    public static string GetTableName<T>(this DbContext context) where T : class
    {
        ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;

        return objectContext.GetTableNameByObject<T>();
    }

Upvotes: 1

Views: 260

Answers (1)

Michael Gunter
Michael Gunter

Reputation: 12811

You should use Func<DbContext, string> instead of Func<string>.

Upvotes: 2

Related Questions