0

I have a model Auto in MVC application that has properties such as

public string Id { get; set; }
public bool IsOOS{ get; set; }
public string Make { get; set; }
public string Model { get; set; }
[XmlElement(IsNullable = true)]
public DateTime? RegisteredDate { get; set; }

and a class that has this ...

var a = new Auto(){
Id = someIDcomingfromServer,
IsOOS = someOOScomingFromServer,
... 
}

what I want to be able to do is... to loop through those and see if any of the properties are now null.

how can I loop through and see if any of those properties (Id, IsOOS etc) contain null?

Thanks

i3arnon
  • 113,022
  • 33
  • 324
  • 344
NoviceDeveloper
  • 1,270
  • 3
  • 15
  • 41
  • Are you wanting to check the _input_ (e.g. `someIDcomingfromServer`) or after the object has been created? And what do you want to do if one of them _is_ null? Set a default value? – D Stanley Sep 08 '14 at 16:15
  • `IsOOS` _can't_ be `null`. – Dave Zych Sep 08 '14 at 16:16
  • After it is created. At this point if any of the properties contain null to capture that value and throw an exception. – NoviceDeveloper Sep 08 '14 at 16:17
  • 1
    If you want to loop dynamically through every property of an object, have a look at reflection: http://stackoverflow.com/questions/4879197/using-reflection-read-properties-of-an-object-containing-array-of-another-object – HaukurHaf Sep 08 '14 at 16:17
  • @HaukurHaf I kinda new about this, and since it is an expensive process, is there any alternative? – NoviceDeveloper Sep 08 '14 at 16:21
  • I would weigh how often you need to do it before discounting reflection. If you're doing this a lot in a critically fast process, then another solution might be better. But check how slow reflection is before discounting it. – tbddeveloper Sep 08 '14 at 16:22

3 Answers3

6

Well you could use reflection to get a collection of all properties and check each for null, but why not just be explicit?

if (Id == null || Make == null || Model == null || RegisteredDate == null)

It's shorter, easier to understand, doesn't have the performance overhead of reflection, and doesn't require that much maintenance. There's not a "magic" function that will tell you if any property of a class is null.

I would be careful not to shorten development time at the expense of system performance. A little extra time spent in development (even if it's tedious) can make a huge difference in system performance.

That said, a single Linq-query would be:

bool hasNull = 
    a.GetType()
     .GetProperties()
     .Any(prop => prop.GetValue(a, null) == null);
D Stanley
  • 149,601
  • 11
  • 178
  • 240
3

You can use reflection for this. Get all PropertyInfos from your instance and check their values. Something like this:

foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
{
    object value = pinfo .GetValue(obj, null);
}

Beware, reflection is an expensive process.

Oscar
  • 13,594
  • 8
  • 47
  • 75
1

You can serialize it and search the string for /> since the serializer outputs nulls as and empties as . This may catch some empty objects as well as null objects. If you only want nulls, you'd need to set them as nullable and look for xsi:nil="true" in the attributes.

*I posted this solution because you wanted another option. The recommendations from the others are the advised way to do it.

VoteCoffee
  • 4,692
  • 1
  • 41
  • 44