【问题标题】:c# generic Delegate Error Cannot bind to the target method because its signaturec# generic Delegate Error Cannot bind to the target method because its signature
【发布时间】:2013-09-30 19:19:26
【问题描述】:

我有一个通用委托,它在这样的单例类中实现;

public class BBCacheMethods
{
    private static BBCacheMethods instance;

    private BBCacheMethods() { }

    public static BBCacheMethods Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new BBCacheMethods();
            }

            return instance;
        }
    }

    public T GetResUstPolitikaBelgeTurlerisForCache<T>() where T:class
    {
        return null;
    }

    public delegate T MethodToInvokeDelegate<T>();
    public static MethodToInvokeDelegate<T> MethodToInvoke<T>() where T : class
    {  
        MethodInfo method = Instance
            .GetType()
            .GetMethod("GetResUstPolitikaBelgeTurlerisForCache");

        if (null != method)
        {
            return (MethodToInvokeDelegate<T>)Delegate.CreateDelegate(
                typeof(MethodToInvokeDelegate<T>),
                Instance,
                method);
        }
        else
            throw new FaultException("");
    }

当我调用委托时收到此错误:

无法绑定到目标方法,因为它的签名或安全透明度与委托类型的不兼容。

调用委托的类是这样调用的:

public static void loadCache<T>() where T : class
{
    var aa = BBCacheMethods.MethodToInvoke<T>();
}

我不知道为什么会收到这样的错误,感谢任何帮助。

【问题讨论】:

    标签: c# generics delegates


    【解决方案1】:

    我不认为你想要的实际上是可能的。委托规范中使用的通用参数 T 必须与 GetResUstPolitikaBelgeTurlerisForCache 和 MethodToInvoke 方法中的 T 相同,但框架不能假设这一点。

    我得到了一些使用通用单例类的工作:

    public class BBCacheMethods<T> where T:class
    {
        private static BBCacheMethods<T> instance;
    
        private BBCacheMethods() { }
    
        public static BBCacheMethods<T> Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new BBCacheMethods<T>();
                }
                return instance;
            }
        }
    
    
        public T GetResUstPolitikaBelgeTurlerisForCache()
        {
            return null;
        }
    
    
        public delegate T MethodToInvokeDelegate();
    
        public static MethodToInvokeDelegate MethodToInvoke()
        {
    
            MethodInfo method = Instance.GetType().GetMethod("GetResUstPolitikaBelgeTurlerisForCache");
            if (null != method)
            {
                return (MethodToInvokeDelegate)Delegate.CreateDelegate(typeof(MethodToInvokeDelegate), Instance, method);
            }
            else
                throw new FaultException("");
        }
    

    然后您可以按如下方式调用它:

    var aa = BBCacheMethods<T>.MethodToInvoke();
    var result = aa();
    

    【讨论】:

    • 你可能是对的,它工作正常,但我正在寻找这个签名 T GetResUstPolitikaBelgeTurlerisForCache() 因为我不想用 T 装饰整个班级。
    • 查看我的更新。您可以改为将所需的代码放在一个单独的类中并从您的单例类中聚合它,这样您就不必使用泛型类型参数“装饰”您的单例类。
    猜你喜欢
    • 2019-02-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多