动态地将一种类型的代理转换为另一种 [英] Dynamically casting one type of delegate to another
问题描述
我使用反射来抓取一个恰好是代理的字段。我需要替换这个委托我自己的,但委托的类型是私人的(所以我不能从我的方法创建它,并分配它)
I'm using reflection to grab a field that happens to be a delegate. I need to replace this delegate with my own, but the type of the delegate is private (so I can't create it from my method and assign it)
我有一个具有完全匹配的签名的委托类型,所以有一些方法我可以动态地转让我的委托到这个其他类型?我有一个Type对象表示未知类型。
I have a delegate type with an exactly matching signature, so is there some way I can dynamically cast my delegate to this other type? I have a Type object representing the unknown type.
我意识到上面所说的可能不是很清楚,所以这里有一些代码:
I realize what I've said above may not very clear, so here's some code:
var delegate_type = Assembly.GetAssembly(typeof(A.F))
// public delegate in A.ZD (internal class)
.GetType("A.ZD+WD");
A.ZD + WS
(混淆名称)委托是 void(System.Drawing.Graphics)
。
The type signature of the A.ZD+WS
(obfuscated name) delegate is void(System.Drawing.Graphics)
.
Is there a way I can cast an Action<Graphics>
to this delegate type?
推荐答案
可以向此委托类型转换 Action< Graphics& 本文似乎有您想要的。
这篇关于动态地将一种类型的代理转换为另一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!