C#反射:GetMethods(..)无法检索任何内容
发布时间:2020-12-16 00:10:11 所属栏目:百科 来源:网络整理
导读:我目前正在尝试获得一组非常具体的方法,但我没有这样做. 我需要从实现特定接口的所有类中获取与特定签名匹配的所有方法. 到目前为止我得到的是: IEnumerableSystem.Type classes = Assembly.GetAssembly(typeof(IActionMethod)).GetTypes().Where(x = x.Get
我目前正在尝试获得一组非常具体的方法,但我没有这样做.
我需要从实现特定接口的所有类中获取与特定签名匹配的所有方法. 到目前为止我得到的是: IEnumerable<System.Type> classes = Assembly.GetAssembly(typeof(IActionMethod)).GetTypes().Where(x => x.GetInterface("IActionMethod") != null); MethodInfo[] methods; List<MethodInfo> relevant; ParameterInfo[] parameters; foreach(System.Type cls in classes) { methods = cls.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public); relevant.Clear(); for(int i = 0; i < methods.Length; i++) { parameters = methods[i].GetParameters(); if(parameters.Length == 1 && parameters[0].GetType() == typeof(GameObject) && methods[i].ReturnType == typeof(void)) relevant.Add(methods[i]); } } 此代码已在GetMethods(..)失败,但未返回任何方法. 界面本身不包含任何内容,我只是用它来“标记”相关的类,因为我无法提出任何其他解决方案. 任何人都可以告诉我为什么界面渲染GetMethodsuseless或指向我上面的代码中的错误? 解决方法
我怀疑这是问题所在:
foreach(System.Type cls in classes) { methods = cls.GetType().GetMethods(...) cls已经是一个Type,因此在其上调用GetType()将返回System.Type(或子类).我怀疑你只想要: foreach(System.Type cls in classes) { methods = cls.GetMethods(...) 还不清楚为什么要在每次迭代时清除相关列表.这意味着最后的唯一条目将是你看到的最后一堂课 – 你确定这是你想要的吗? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |