简体   繁体   中英

Delegate does not contain a definition for 'CreateDelegate'

Using Unity 2018-2017 with same problem on building for net- error CS0117: 'Delegate' does not contain a definition for 'CreateDelegate' This is the method:

 private V CreateDelegate<V>(MethodInfo method, Object target) where V : class
    {

        var ret = (Delegate.CreateDelegate(typeof(V), target, method) as V);

        if (ret == null)
        {
            throw new ArgumentException("Unabled to create delegate for method called " + method.Name);
        }
        return ret;

    }

Building for UWP. Using system.Linq I tryed with "MethodInfo" but maybe some parameters are wrong. This method isn´t available?

Which platform/runtime are you targeting? I don't know about Mono, but .Net standard 1.x doesn't support Delegate.CreateDelegate. Always keep in mind that you're writing your code against a limited subset of the .Net framework. Also keep in mind that your code will inevitably be AOT-compiled on some platforms (il2cpp, iOS, etc.), so some reflection/emit features will be unavailable.

Note: AOT means ahead-of-time, meaning your code is compiled to machine instructions rather than an intermediate language. Reflection is when you use the code itself as data, so for example you can get a list of the properties a class defines. Emit means generating code at runtime. If you don't understand what those are, you should probably do some studying. It's well worth the effort in the long run.

1. Your return type is a class, not a delegate.

where V : class

So this method doesn't even make sense. You're going to get an invalid cast exception.

2. CreateDelegate takes 2 parameters, not 3.

I'm not even sure what purpose target even serves here, so I can't even guess what you're trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM