2017-05-09 46 views
5

我试图在dotNet核心Web应用程序中使用代表进行反射。以下是代码示例。代表不包含.net核心中CreateDelegate的定义

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

编译器提供了以下错误:

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

是否有变通方法在.NET中创建核心代表?

回答

4

改为使用MethodInfo.CreateDelegate

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

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

Delegate.CreateDelegate预计在.NET核2.0返回:.NET API Catalog