I want to do something like this question, but it isn't working as I'm expecting.
I have a list of an object's properties and their types, an unknown object, and a list of default value types that I want to ignore from the object.
public void GetOnlyNonDefault(object oObject, IgnoreValues ignoreValues)
{
//properties is set beforehand
for (int i = 0; i < properties.Count; i++)
{
object originalValue =
oObject
.GetType()
.GetProperty(properties[i].Name)
.GetValue(oObject, null);
//retrieving the default value I want to ignore for this property
object ignoreValue = ignoreValues.GetDefaultOf(properties[i]);
//Problem here
//This doesn't work
if (originalValue == ignoreValue) { continue; }
//This works for nearly everything BUT enums
if (ValueType.Equals(originalValue, ignoreValue)) { continue; }
}
}
I've read from here that enums' default value is zero, and that's what I'm returning from GetDefaultOf
when the type is an enum.
How can I do a compare by values for all the value types including enums in the above code?
Edit: May be relevant, but the enums I'm using in the oObject
class don't have any value set to 0, they all start at 1.
public enum UserStatus
{
Regular = 1, Manager = 2, Director = 3, Admin = 4
}
Edit²: Here's a basic version of the code I'm trying to do giving the results I'm experiencing: http://pastebin.com/rfJA9CGp
I can't really change the enums because that's the point of this code, I don't know what is the object at runtime nor it's properties, but I want to figure any values different from the defined default (the class IgnoreValues
contains the default values for all value types).
I can't cast to int
when it's an enum because then I'd ignore the case where an enum with 0
was actually defined in the code, so I'm looking for any way to compare the values.