I am looking for a way to set 'null' property values to a 'non-null' value. These properties are associated with an object and there is a list of multiple objects.
The issue i am having is with converting the 'null' value to a 'non-null' value where each property has a different type.
What i have so far is a few nested loops and conditionals in an attempt to identify the null properties and set them to non-null.
//loop through each object
for (int i = 0; i < objectList.Count; i++)
{
//loop through each object and each field within that object
foreach (var property in objectList[i].GetType().GetProperties())
{
var current_field_val = property.GetValue(objectList[i], null);
//null validation
if (current_field_val == null)
{
PropertyInfo current_field_data_type = objectList[i].GetType().GetProperty(property.Name);
if (current_field_data_type is String)
{
objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], "");
}
else if (current_field_data_type is int)
{
objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], 0);
}
else if (current_field_data_type is double)
{
objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], 1);
}
else if (current_field_data_type is object)
{
objectList[i].GetType().GetProperty(property.Name).SetValue(objectList[i], "");
}
}
}
}
Excuse my poor indenting, VS didn't play nice when copying back and forth.