C# 动态调用DLL 调用多重载方法

DLL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class Class1
    {
        public void Function(string parameter1)
        {
            Console.Write("一个字符串型形参");
        }
        public void Function(int parameter1)
        {
            Console.Write("一个整数型形参");
        }
        public void Function(string parameter1,string paramter2)
        {
            Console.Write("两个字符串型形参");
        }
    }
}

应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Assembly assembly = Assembly.LoadFrom("./ClassLibrary1.dll");
            Type type = assembly.GetType("ClassLibrary1.Class1");
            object instance= Activator.CreateInstance(type);

            Console.WriteLine(instance.GetType().GetMethod("Function", new Type[] { typeof(int) }).Invoke(instance,new object[] { 0}));
            Console.WriteLine(instance.GetType().GetMethod("Function", new Type[] { typeof(string) }).Invoke(instance,new object[] { ""}));
            Console.WriteLine(instance.GetType().GetMethod("Function", new Type[] { typeof(string), typeof(string) }).Invoke(instance, new object[] { "",""}));
            Console.ReadKey();
        }
    }
}

你可能感兴趣的:(c#,开发语言)