简体   繁体   中英

Delegate does not contain a definition for CreateDelegate in .net core

I'm trying to use delegates for reflection in dotNet core web application. Below is the sample of the code.

Action action = (Action) Delegate.CreateDelegate(typeof(Action), method)

Compiler gives the following error:

'Delegate' does not contain a definition for 'CreateDelegate'   ConsoleApp2..NETCoreApp,Version=v1.0'

Is there any work around for creating delegates in .net Core?

Use MethodInfo.CreateDelegate instead.

MethodInfo methodInfo = target.GetType().GetMethod(nameof(FooMethod));

Action action = (Action) methodInfo.CreateDelegate(typeof(Action), target);

Delegate.CreateDelegate is expected to return in .NET Core 2.0: .NET API Catalog

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