我正在尝试加载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)));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句