簡體   English   中英

反射從DLL中的所有類型獲取價值

[英]reflection get value from all types in DLL

我正在嘗試加載DLL,並為每種類型顯示屬性名稱和值。 我有以下代碼,該代碼引發System.Reflection.TargetException和消息“對象與目標類型不匹配”。

var DLL = Assembly.LoadFrom(PathToDLL);
                foreach (Type type in DLL.GetTypes())
                {
                    foreach (PropertyInfo property in type.GetProperties())
                    {
                        lstProperties.Items.Add(string.Format("{0} - {1}", property.Name, property.GetValue(DLL, null)));
                    }
                }

我嘗試通過GetValue()調用使用類型,DLL和null,我在做什么錯呢?

我要測試的DLL具有一個屬性:

public string TheValue{ get; set; }

謝謝大家

擴展評論,這里是他們正在談論的示例。

在DLL中:

public class TheClass
{
    public string TheValue { get; set; }
}

創建該類的實例並設置屬性值:

TheClass cls = new TheClass();
cls.TheValue = "hello!";

然后更改代碼以引用您創建的實例:

lstProperties.Add(string.Format("{0} - {1}", property.Name, property.GetValue(cls, null)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM