0

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.

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

+1 - you might also want to add exception handling in your GetValue call as the callee might implement a custom getter that could yield an exception.
It worked perfectly, thanks. If anyone else needs this note that there's an error in the code I posted with the question. To check for the custom attribute I had to iterate between all the attributes to find the one I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.