如何在C#中“返回一个方法”中出现一个方法。 [英] How can I occure a method in C# "returning a methods"

查看:59
本文介绍了如何在C#中“返回一个方法”中出现一个方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我有100个方法,方法名称如下:

method1(),method2(),method3()..... method100()



我想退回我的方法1到100





  public   static  returningMethod()
{

for (i = 0 ; i< 100; i ++)
{
// 如何退回metods ??
}

}

解决方案

使用反射



http://www.c-sharpcorner.com/Blogs/8856/getting- all-methods-of-class-using-reflection-C-Sharp.aspx [ ^ ]



http://stackoverflow.com/questions/2202381/reflection-how-to-invoke-method-with-parameters [ ^ ]



但是你要做的通常是应用程序设计不好而应该重新考虑的结果。


使用反思



 public void调用<   T  > (string methodName)其中T:new()
{
T instance = new T( );
MethodInfo方法= typeof(T).GetMethod(methodName);
method.Invoke(instance,null);
}


 使用 System.Reflection; 
命名空间 linqpractice
{

class MyClass
{

public static int method1()
{
return 1 ;
}
public static string method2()
{
return 你好;
}
public static double method3()
{
return 1 5 ;
}
}
class 计划
{

static void Main( string [] args)
{


// linqpractice namespace
// MyClass是其中存在method1(),method2()和method3()的类
类型calledType = Type.GetType( linqpractice.MyClass);
ArrayList ob = new ArrayList();
for int i = 1 ; i < = 3 ; i ++)
{
ob。添加(calledType.InvokeMember(
method + i.ToString(),
BindingFlags.InvokeMethod | BindingFlags.Public |
BindingFlags.Static,
null
null
null ));

}
// 打印方法的返回值
foreach var item in ob)
{
Console.WriteLine(item);
}


}
}



}


Hi, For example I have 100 methods and the methods names so:
method1(), method2(), method3() ..... method100()

I want to returning my methods 1 to 100


public static returningMethod()
{

   for(i=0;i<100;i++)
   {
      //How can I return the metods ??
   }

}

解决方案

Use reflection

http://www.c-sharpcorner.com/Blogs/8856/getting-all-methods-of-class-using-reflection-C-Sharp.aspx[^]

http://stackoverflow.com/questions/2202381/reflection-how-to-invoke-method-with-parameters[^]

However what you're trying to do is normally a result of bad application design that should be re-thought.


Use Reflection

public void Invoke<T>(string methodName) where T : new()
{
    T instance = new T();
    MethodInfo method = typeof(T).GetMethod(methodName);
    method.Invoke(instance, null);
}


using System.Reflection;
namespace linqpractice
{

  class MyClass
    {

        public static int method1()
        {
            return 1;
        }
        public static string method2()
        {
            return "Hello";
        }
        public static double method3()
        {
            return 1.5;
        }
    }
 class Program
    {
      
        static void Main(string[] args)
        {


            //linqpractice namespace
            //MyClass is the class where method1(),method2() and method3() are present
            Type calledType = Type.GetType("linqpractice.MyClass");
            ArrayList ob = new ArrayList();
            for (int i = 1; i <= 3; i++)
            {
                ob.Add(calledType.InvokeMember(
                             "method" + i.ToString(),
                             BindingFlags.InvokeMethod | BindingFlags.Public |
                                 BindingFlags.Static,
                             null,
                             null,
                             null));

            }
            //printing the returns value of the methods
            foreach (var item in ob)
            {
                Console.WriteLine(item); 
            }
            
           
        }
    }



}


这篇关于如何在C#中“返回一个方法”中出现一个方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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