错误绑定到目标方法 [英] Error binding to target method

查看:137
本文介绍了错误绑定到目标方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从一个助手类调用一个静态方法,其中类型在运行时才是不知道的。我以为我已经解决了问题,但是我收到以下错误 -



绑定到目标方法时出错。



谁能看到这段代码有什么问题?任何帮助将不胜感激。

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

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

上面的类型objecttype可以是任何类,RepositoryStringExtensions是包含方法,它试图调用的方法显示在下面 -

  public static string GetTableName&T;(此DbContext上下文)其中T:class 
{
ObjectContext objectContext =((IObjectContextAdapter)context).ObjectContext;

return objectContext.GetTableNameByObject< T>();
}


解决方案

您应该使用 Func< DbContext,string> 而不是 Func< string>


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

解决方案

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

这篇关于错误绑定到目标方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆