如何使用反射调用带有 0 个参数的非公共方法? [英] How do I use reflection to call a non public method with 0 arguments?

查看:51
本文介绍了如何使用反射调用带有 0 个参数的非公共方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类有两个类似这样的方法:

My class has two methods that look like this:

void Update()
void Update(string)

通过反射,我想调用顶部的,但是当我尝试这样做时:

With reflection, I want to call the top one, but when I try this:

            const BindingFlags findFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

            var methodInfo = script.GetType().GetMethod(methodName, findFlags);

我收到一个 AmbiguousMatchException.如何调用 no arg 方法?

I get a AmbiguousMatchException. How do I call the no arg method?

推荐答案

以下是您的操作方法:

            var methodInfo = script.GetType().GetMethod("Update", findFlags, null, new Type[] { }, null);

这篇关于如何使用反射调用带有 0 个参数的非公共方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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