【发布时间】:2023-04-02 18:41:01
【问题描述】:
我正在尝试创建一个 nUnit 测试来执行以下操作:
1) 加载要测试的 DLL。
2) 在各种类型之间进行迭代。
3) 找到具有特定自定义属性的那些。
4) 实例化这些类型并确保它们的所有公共属性都不为空。
这是我到目前为止写的:
Assembly assembly = Assembly.LoadFile("MyLib.dll");
foreach (Type type in assembly.GetTypes()) {
if (type.GetCustomAttributes(typeof(CustomAttribute), false).Length != 0) {
Object instance = Activator.CreateInstance(type);
foreach (PropertyInfo propertyInfo in type.GetProperties()) {
// how to go on from here?
}
}
}
如您所见,假设其余代码正确,我不知道如何通过测试空值来完成。
【问题讨论】:
标签: c# .net reflection properties attributes