There is a express x=> x.delegate += null
, how to get target methodinfo
from this express. I know by il
file , but it's not so friendly,
anyone has any reasonable solution?
EventInfo GetTargetMethodInfo(Delegate d)
{
EventInfo info = null;
var ilBytes = d.Method.GetMethodBody().GetILAsByteArray();
var callIdx = Array.IndexOf(ilBytes, (Byte)OpCodes.Ldnull.Value);
var calledMethodEntry = Array.FindIndex(ilBytes, x => x == OpCodes.Callvirt.Value || x == OpCodes.Call.Value);
if (callIdx > 0 && callIdx < ilBytes.Length
&& calledMethodEntry > callIdx)
{
var pos = calledMethodEntry + 1;
var methodCode = (((ilBytes[pos++] | (ilBytes[pos++] << 8)) | (ilBytes[pos++] << 0x10)) | (ilBytes[pos++] << 0x18));
var mi = d.Method.Module.ResolveMethod(methodCode);
if (mi != null && mi.Name.StartsWith("add_"))
{
var evtName = mi.Name.Substring(4);
info = mi.DeclaringType.GetEvent(evtName);
}
}
return info;
}
Aucun commentaire:
Enregistrer un commentaire