给定一个程序集包含
namespace Foo{public class Bar;}
我如何创建 Action<Foo.Bar>
来自另一个程序集而不在编译时引用第一个程序集?
最佳答案
如果你使用
Type barType = Type.GetType("Foo.Bar, whateverassembly");
Type actionType = typeof(Action<>).MakeGenericType(barType);
actionType
现在将委托(delegate) Action<Foo.Bar>
.但是,要使用它,您需要继续使用反射,因此您需要找到 MethodInfo
。符合签名 void(Foo.Bar)
, 并调用 Delegate.CreateDelegate
创建一个委托(delegate)。你需要 Delegate.DynamicInvoke
执行它。
Delegate call = Delegate.CreateDelegate(actionType, ...);
...
call.DynamicInvoke(someBar);
有件事告诉我这不是你想的...
关于c# - 通过反射创建委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3024253/