項目結構:

將Document類庫生成的DLL文件放在ConsoleApplication2\ConsoleApplication2\bin\Debug文件夾下
-------------------------------------------------------------Document.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Document
{
public class Document
{
public string Name = "李四";
public string GetName(string name)
{
return name;
}
}
}-------------------------------------------------------------Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//獲取程序集
Assembly asb = Assembly.LoadFrom(Directory.GetCurrentDirectory() + "/Document.dll");
//獲取程序集下面的Document類
Type document = asb.GetType("Document.Document");
//實例化Document類對象(有參數的話需要傳遞object參數)
object docObj = Activator.CreateInstance(document);
//獲取Document類中的方法
MethodInfo mi = document.GetMethod("GetName");
//參數
object[] parameter = new object[] { "張三" };
Console.WriteLine(mi.Invoke(docObj, parameter).ToString());//輸出方法的返回值:張三
//獲取Document類中的屬性
FieldInfo pi = document.GetField("Name");
Console.WriteLine(pi.GetValue(docObj));//輸出方法的屬性:李四
Console.ReadKey();
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。