I'm using reflection to determine how to format properties on an object.
Here's a dictionary of formatters
:
var formatters = new Dictionary<Type, Func<object, object>>()
{
{ typeof(Enum), x => ((Enum)x)?.FormatEnum() },
{ typeof(DateTime?), x => ((DateTime?)x)?.ToShortDateString() },
{ typeof(string), x => x.ToString().ToLower() }
};
Most types will work fine, however when I get the type of my nullable enum TaskStatus
, the type is not "Enum", it's the name of the type.
I noticed the base type is "Enum", however I don't want to have to check the base type since most other types will work just fine with the type.
For example, checking the type of string
works fine:
typeof(string).FullName = "System.String"
But for the TaskStatus
enum:
typeof(Enums.TaskStatus).FullName = "Enums.TaskStatus"
So typeof(Enum) != typeof(Enums.TaskStatus)
which is what is preventing the dictionary from finding a match.
Is there any way to find a match in the formatters
dictionary without having to put in all the different nullable enum type names I am using?
Edit:
Here's my entire code:
public class NoteHistory
{
[JsonProperty("Status")]
public TaskStatus? TaskStatus { get; set; }
[JsonProperty("CreatedDate")]
public DateTime? CreatedDate { get; set; }
[JsonProperty("Description")]
public string Description { get; set; }
public bool IsDeleted { get; set; } // not interested in comparing this
}
public enum TaskStatus
{
ToDo,
Done
}
public static void DisplayValues()
{
var noteHistory1 = new NoteHistory
{
TaskStatus = TaskStatus.ToDo
};
var noteHistory2 = new NoteHistory
{
TaskStatus = TaskStatus.Done
};
// Get all properties with JsonProperty attr.
var propertiesToCompare = typeof(NoteHistory)
.GetProperties()
.Where(x => x.CustomAttributes
.Any(y => y.AttributeType.Name == nameof(JsonPropertyAttribute)));
var comparers = new Dictionary<Type, Func<object, object, bool>>
{
{ typeof(string), (x, y) => !string.Equals(x?.ToString(), y?.ToString(), StringComparison.InvariantCultureIgnoreCase) }
};
var formatters = new Dictionary<Type, Func<object, object>>()
{
{ typeof(Enum), x => ((Enum)x)?.FormatEnum() }, // doesn't work
{ typeof(DateTime?), x => ((DateTime?)x)?.ToShortDateString() },
{ typeof(string), x => x.ToString().ToLower() },
};
foreach (var propertyToCompare in propertiesToCompare)
{
var prop1Value = propertyToCompare.GetValue(noteHistory1);
var prop2Value = propertyToCompare.GetValue(noteHistory2);
Func<object, object, bool> comparer;
comparers.TryGetValue(propertyToCompare.PropertyType, out comparer);
var hasChanged = comparer?.Invoke(prop1Value, prop2Value) ?? prop1Value != prop2Value;
if (hasChanged)
{
Func<object, object> formatter;
formatters.TryGetValue(propertyToCompare.PropertyType, out formatter);
var formattedValue = formatter?.Invoke(noteHistory2);
// TODO: display the value
}
}
}