Error binding to target method

大兔子大兔子 提交于 2020-01-15 09:13:33

问题


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>();
    }

回答1:


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



来源:https://stackoverflow.com/questions/17026259/error-binding-to-target-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!