Retrieving the name of the invoked method executed in a Func

前端 未结 3 1736
时光取名叫无心
时光取名叫无心 2020-12-18 15:03

I would like to get the name of the method that is being delegated as a Func.

Func func = x => x.DoSomeMethod();
string name = Ext         


        
3条回答
  •  渐次进展
    2020-12-18 15:35

    Look Ma! No expression trees!

    Here's a quick, dirty and implementation-specific version that grabs the metadata token from the IL stream of the underlying lambda and resolves it.

    private static string ExtractMethodName(Func func)
    {
        var il = func.Method.GetMethodBody().GetILAsByteArray();
    
        // first byte is ldarg.0
        // second byte is callvirt
        // next four bytes are the MethodDef token
        var mdToken = (il[5] << 24) | (il[4] << 16) | (il[3] << 8) | il[2];
        var innerMethod = func.Method.Module.ResolveMethod(mdToken);
    
        // Check to see if this is a property getter and grab property if it is...
        if (innerMethod.IsSpecialName && innerMethod.Name.StartsWith("get_"))
        {
            var prop = (from p in innerMethod.DeclaringType.GetProperties()
                        where p.GetGetMethod() == innerMethod
                        select p).FirstOrDefault();
            if (prop != null)
                return prop.Name;
        }
    
        return innerMethod.Name;
    }
    

提交回复
热议问题