开发者

Testing properties with reflection using attributes

开发者 https://www.devze.com 2022-12-17 14:31 出处:网络
I\'m trying to create a nUnit test to do the following: 1) Load the DLL to test. 2) Iterate among the various types.

I'm trying to create a nUnit test to do the following:

1) Load the DLL to test.

2) Iterate among the various types.

3) Find the ones that have a certain custom attribute.

4) Instantiate these types and make sure that all their public properties aren't null.

Here's what I wrote so far:

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?
        }  
 开发者_开发知识库   }  
}  

As you can see I don't know how to finish by testing for nulls, assuming that the rest of the code is correct.


Gettingn the value works this way:

object value = propertyInfo.GetValue(instance, null);

if (value == null)
   //Null value
else if (DBNull.Value.Equals(value))
   //DB Null
0

精彩评论

暂无评论...
验证码 换一张
取 消