我希望能够在运行时执行以下等效操作:
var action = new Action<ANYTHING AT RUNTIME>(obj => Console.WriteLine("Called = " + obj));
我知道我需要为 Action 获取正确的类型,但不确定如何使用 Delegate.Create 获取最终位。Type
表示动作定义中的 T。
var actionType = typeof(Action<>).MakeGenericType(Type);
var constructor = actionType.GetConstructors()[0];
var @delegate = Delegate.CreateDelegate(actionType, <WHAT GOES HERE>);
人们似乎缺少的一点是我正在尝试创建一个无法静态指定 T 的 Action 实例,因为它是从派生自 Attribute 的类中使用的 - 这意味着 T 可以是任何东西,并且不能定义为通用定义
干杯