C#加载一个本机dll动态简单
本文关键字:本机 dll 动态 简单 一个 加载 | 更新日期: 2023-09-27 17:58:52
所以我不知道该怎么做。。。
我有一个程序"Prg.cs"和一个dll"Test.dll"。我试过了:
Assembly asm=Assembly.Load(@"C:'Users'Me'documents'visual studio 2013'Projects'Prg'Prg'bin'Debug'Test.dll");
Type runApp = asm.GetType();
dynamic thisApp = Activator.CreateInstance(runApp, this);
但给我一个错误:
An unhandled exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll
Additional information: Could not load file or assembly 'C:''Users''Me''documents''visual studio 2013''Projects''Prg''Prg''bin''Debug''Test.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
谢谢!
您可以这样做:
class Program
{
static void Main(string[] args)
{
var dll = Assembly.LoadFile(@"C:'Test.dll");//The path of your dll
var theType = dll.GetType("dll.Test");
var c = Activator.CreateInstance(theType);
var method = theType.GetMethod("Output");//Your method in the class
method.Invoke(c, new object[]{@"Hello world"});
Console.ReadLine();
}
}
该链接可以帮助您粘贴overflow.com/a/18362515/3383479