使用Reflection来获取排除C#7.0中本地函数的方法?

使用Reflection来获取排除C#7.0中本地函数的方法?,第1张

概述有没有办法使用反射来获取类中的私有静态方法,而无需在这些方法中定义任何本地函数? 例如,我有一个这样的类: public class Foo { private static void FooMethod(){ void LocalFoo(){ // do local stuff } // do foo stuff 有没有办法使用反射来获取类中的私有静态方法,而无需在这些方法中定义任何本地函数?

例如,我有一个这样的类:

public class Foo {    private static voID FooMethod(){        voID LocalFoo(){           // do local stuff        }        // do foo stuff    }}

如果我使用反射来抓取私有静态方法,如下所示:

var methods = typeof(Foo).getmethods(BindingFlags.Static|BindingFlags.NonPublic)    .Select(m=>m.name).ToList();

然后我最终得到了类似的东西:

FooMethod<FooMethod>g__LocalFoo5_0

使用gnarly编译器生成的本地函数名称.

到目前为止,我能够想到的最好的方法是添加一个Where子句来过滤掉本地函数,例如:

var methods = typeof(Foo).getmethods(BindingFlags.Static|BindingFlags.NonPublic)        .Where(m=>!m.name.Contains("<")        .Select(m=>m.name).ToList();

要么:

var methods = typeof(Foo).getmethods(BindingFlags.Static|BindingFlags.NonPublic)        .Where(m=>!m.name.Contains("__")        .Select(m=>m.name).ToList();
解决方法 关于什么:

var methods = typeof(Foo).getmethods(BindingFlags.Static | BindingFlags.NonPublic)    .Where(x => !x.IsAssembly)    .Select(x => x.name)    .ToList();

结果:

"FooMethod"

IsAssembly属性摘要:

Gets a value indicating whether the potential visibility of this method or constructor is described by System.Reflection.MethodAttributes.Assembly; that is,the method or constructor is visible at most to other types in the same assembly,and is not visible to derived types outsIDe the assembly.

总结

以上是内存溢出为你收集整理的使用Reflection来获取排除C#7.0中本地函数的方法?全部内容,希望文章能够帮你解决使用Reflection来获取排除C#7.0中本地函数的方法?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1229022.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存