CSharp等效于java.lang.reflect.Method [英] CSharp Equivalent of java.lang.reflect.Method

查看:67
本文介绍了CSharp等效于java.lang.reflect.Method的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的

我正在寻找java.lang.reflect.Method的等效项,并找到下面的解决方案

I am looking for the equivalent of java.lang.reflect.Method and found out the solution below

MethodInfo[] methods = typeof(Class).GetMethods();

// Example 
foreach (MethodInfo info in methods)
            {
                Console.WriteLine(info.Name);

                // Call Win method.
                if (info.Name == "Win")
                {
                    info.Invoke(program, null); // [2]
                }
            }

它正在工作,但不知道它是否正确使用.

It is working but don't know it is correct usage or not.

我希望有人会建议我证明这一点.

I hope someone will advise me to get it prove.

感谢和问候

推荐答案

您可以通过调用获取所需的方法,而不是对所有方法使用循环具有方法名称的GetMethod(如Java).

Instead of using a loop over all the methods, you can get the method you want by calling GetMethod with the name of the method (as in Java).

您还可以使用InvokeMember直接调用该方法.

You can also directly invoke the method by using InvokeMember.


这篇关于CSharp等效于java.lang.reflect.Method的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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