使用 MethodInfo 调用带参数的方法

参考来自链接 https://www.cnblogs.com/CodingHiding/articles/3328362.html

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excute_code_1();
        }

        private void Excute_code_1()
        {
            CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
            ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();

            CompilerParameters objCompilerParameters = new CompilerParameters();

            //添加需要引用的dll
            objCompilerParameters.ReferencedAssemblies.Add("System.dll");
            objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");

            //是否生成可执行文件
            objCompilerParameters.GenerateExecutable = false;

            //是否生成在内存中
            objCompilerParameters.GenerateInMemory = true;

            //编译代码
            CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, textBox1.Text);

            if (cr.Errors.HasErrors)
            {
                var msg = string.Join(Environment.NewLine, cr.Errors.Cast().Select(err => err.ErrorText));
                MessageBox.Show(msg, "编译错误1");

                var msg1 = string.Join(Environment.NewLine, cr.Errors.Cast().Select(err => err.ErrorNumber));
                MessageBox.Show(msg1, "编译错误2");

                var msg2 = string.Join(Environment.NewLine, cr.Errors.Cast().Select(err => err.Line));
                MessageBox.Show(msg2, "编译错误3");
            }
            else
            {
                Assembly objAssembly = cr.CompiledAssembly;
                object objHelloWorld = objAssembly.CreateInstance("Test");
                if (objHelloWorld == null)
                {
                    Console.WriteLine("Instance Test is null!");
                    return;
                }

                MethodInfo objMI = objHelloWorld.GetType().GetMethod("method_3");
                if (objMI == null)
                {
                    Console.WriteLine("the method_3 method of Instance Test can not be invoked!");
                    return;
                }

                object[] l_args = new object[2] { 1, 2 };
                MessageBox.Show(objMI.Invoke(objHelloWorld, l_args).ToString());
                //MessageBox.Show(objMI.Invoke(objHelloWorld, new object[]{1,2}).ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            init_code_1();
        }

        private void init_code_1()
        {
            textBox1.Clear();
            textBox1.AppendText("using System.Windows.Forms;\r\n");
            textBox1.AppendText("public class Test\r\n");
            textBox1.AppendText("{\r\n");
            textBox1.AppendText("   public int method_3(int a,int b)\r\n");
            textBox1.AppendText("   {\r\n");
            textBox1.AppendText("       return a + b; \r\n");
            textBox1.AppendText("   }\r\n");
            textBox1.AppendText("}\r\n");
        }
    }
}

 

你可能感兴趣的:(c#)