Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# generic Delegate Error Cannot bind to the target method because its signature

I have a generic delegate which is implemented in side a singleton class like this;

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

The point that when I invoke the delegate I receive this error:

Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.

The class that calls the delegate call it in this way:

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

I did not find out why I do receive such an error, any help is appreciated.

like image 315
MuhanadY Avatar asked Nov 11 '22 21:11

MuhanadY


1 Answers

I don't think that what you want is actually possible. The generic argument T used in the delegate specification must be the same T as in the method GetResUstPolitikaBelgeTurlerisForCache and MethodToInvoke but the framework can't assume this.

I got something working which uses a generic singleton class:

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

You can then invoke it as follows:

var aa = BBCacheMethods<T>.MethodToInvoke();
var result = aa();
like image 80
helb Avatar answered Nov 14 '22 23:11

helb