托管Dll动态调用

来源:互联网 发布:富云软件科技有限公司 编辑:程序博客网 时间:2024/05/20 04:12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Reflection;


namespace CallDll
{
    class Program
    {
        static void Main(string[] args)
        {//E:\learnProg\CSharp\DyCalledDll\DyCalledDll\bin\x64\Release\DyCalledDll.dll
            Assembly ass = Assembly.LoadFile(args[0]);//这里是动态库的路径。
            Type tp = ass.GetType("DyCalledDll.Class1");//dllType是你所需要调用的动态库文件的命名空间+类名(NameSpace.Class)
            MethodInfo method = tp.GetMethod("SayHello");//需要执行的函数
            object ob = Activator.CreateInstance(tp);//创建对象
            method.Invoke(ob, null);//执行函数,后一个参数即为执行函数需要的参数,若无则为null。


        }
    }

}


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


namespace DyCalledDll
{
    public class Class1
    {
        public void SayHello()
        {
            Console.WriteLine("Hello");
        }
    }
}


0 0
原创粉丝点击