溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

反射示例1-----執行反射dll的方法和獲取屬性的值

發布時間:2020-10-25 11:26:42 來源:網絡 閱讀:3224 作者:1473348968 欄目:編程語言

項目結構:

反射示例1-----執行反射dll的方法和獲取屬性的值

將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();
        }
    }
}

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女