使用反射通过字符串动态调用函数

通过反射得到的是对象

 

 

 

using System;
using System.Reflection;
using System.Globalization;
using Microsoft.CSharp;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Text;

namespace ConsoleApplication4
{
    public class Program
    {
        static void Main(string[] args)
        {
            SayHello obj = new SayHello();
            MethodInfo m = obj.GetType().GetMethod("Say");
            MethodInfo n = obj.GetType().GetMethod("Say1");
            m.Invoke(obj, new object[]{1,1.0});
            string answer = n.Invoke(obj, new object[] { 1, 1.0 }).ToString();
            double answe = double.Parse(answer);
            Console.WriteLine(answe);
            Console.ReadKey(true);
        }
    }
    class SayHello
    {
        public void Say(int a,double b)
        {
            Console.WriteLine("Hello World!");
        }
        public double Say1(int a, double b)
        {
            double c = a + b;
            return c;
        }
    }
}

 

 

 

 

 

你可能感兴趣的:(使用反射通过字符串动态调用函数)