如何识别的System.Reflection匿名方法 [英] How to identify anonymous methods in System.Reflection

查看:142
本文介绍了如何识别的System.Reflection匿名方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过反射识别匿名方法?

How can you identify anonymous methods via reflection?

推荐答案

看方法的属性,并查看是否该方法装饰着 CompilerGeneratedAttribute

Look at the attributes of the method, and see if the method is decorated with CompilerGeneratedAttribute.

匿名方法(以及其他的目的,例如自动实现的属性等)将具有该属性添加

Anonymous methods (as well as other objects, such as auto-implemented properties, etc) will have this attribute added.

例如,假设你有你的类的类型。匿名方法将是:

For example, suppose you have a type for your class. The anonymous methods will be in:

Type myClassType = typeof(MyClass);
IEnumerable<MethodInfo> anonymousMethods = myClassType
    .GetMethods(
          BindingFlags.NonPublic
        | BindingFlags.Public 
        | BindingFlags.Instance 
        | BindingFlags.Static)
    .Where(method => 
          method.GetCustomAttributes(typeof(CompilerGeneratedAttribute)).Any());

这应该返回上定义的任何匿名方法 MyClass的

This should return any anonymous methods defined on MyClass.

这篇关于如何识别的System.Reflection匿名方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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