2017-05-09 61 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