动态地将一种类型的代理转换为另一种 [英] Dynamically casting one type of delegate to another

查看:126
本文介绍了动态地将一种类型的代理转换为另一种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用反射来抓取恰好是一个委托的字段。我需要用我自己的代替这个代理,但代理的类型是私有的(所以我不能从我的方法创建它,并分配它)

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).

有没有办法可以将 Action< Graphics> 转换为此委托类型?

Is there a way I can cast an Action<Graphics> to this delegate type?

推荐答案

这篇文章似乎有你想要的。

这篇关于动态地将一种类型的代理转换为另一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆