Implementations for Handling Empty Guid Values in Objects and Lists
Osama Nasir
Senior Dot Net Developer | Back-End Developer | .NET | ASP.NET CORE | ASP.NET MVC | React | Entity Framework | Angular | SQL | Azure | AWS
Implementations for Handling Empty Guid Values in Objects and Lists
1?? Basic Reflection Approach (Limited)
private void ConvertEmptyGuidsToNull(object obj)
{
if (obj == null) return;
var type = obj.GetType();
foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (!prop.CanWrite) continue; // Skip read-only properties
var value = prop.GetValue(obj);
if (value == null) continue;
// Converts only nullable GUIDs
if (prop.PropertyType == typeof(Guid?) && value is Guid guidValue && guidValue == Guid.Empty)
{
prop.SetValue(obj, null);
}
}
}
?? Why this is NOT ideal?
2?? Enhanced Reflection Approach (Handles Lists and Nested Objects)
private void ConvertEmptyGuidsToNull(object obj)
{
if (obj == null) return;
var type = obj.GetType();
foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (!prop.CanWrite || prop.GetIndexParameters().Length > 0) continue; // Skip read-only & indexed properties
var value = prop.GetValue(obj);
if (value == null) continue;
if (prop.PropertyType == typeof(Guid?) && value is Guid guidValue && guidValue == Guid.Empty)
{
prop.SetValue(obj, null);
}
else if (prop.PropertyType == typeof(Guid) && (Guid)value == Guid.Empty)
{
prop.SetValue(obj, null);
}
else if (value is IEnumerable<object> collection) // Handling List<T> and IEnumerable<T>
{
foreach (var item in collection)
{
ConvertEmptyGuidsToNull(item);
}
}
else if (!prop.PropertyType.IsPrimitive && prop.PropertyType != typeof(string))
{
ConvertEmptyGuidsToNull(value);
}
}
}
?? Pros: ? Converts both Guid and Guid? ? Handles lists and iterates over List<T> ? Recursively processes nested objects
?? Cons:
3?? Optimized Reflection with IList Support (Best Implementation)
private void ConvertEmptyGuidsToNull(object obj)
{
if (obj == null) return;
var type = obj.GetType();
foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (!prop.CanWrite || prop.GetIndexParameters().Length > 0) continue;
var value = prop.GetValue(obj);
if (value == null) continue;
// Convert Empty GUIDs to null
if (prop.PropertyType == typeof(Guid?) && value is Guid g && g == Guid.Empty)
{
prop.SetValue(obj, null);
}
else if (prop.PropertyType == typeof(Guid) && (Guid)value == Guid.Empty)
{
prop.SetValue(obj, null);
}
else if (value is IList list) // Directly process List<T>
{
for (int i = 0; i < list.Count; i++)
{
ConvertEmptyGuidsToNull(list[i]);
}
}
else if (!prop.PropertyType.IsPrimitive && prop.PropertyType != typeof(string))
{
ConvertEmptyGuidsToNull(value);
}
}
}
? Best Performance – Uses IList to modify lists in place. ? Handles Deeply Nested Objects – Recursively processes all objects. ? Avoids Boxing/Unboxing Issues – Works directly on collections. ? Avoids Reflection Errors – Skips indexed properties correctly.
?? Which is the Best Implementation?
?? Final Thoughts
? Always check for IList before iterating over IEnumerable<T> to avoid boxing/unboxing overhead. ? Reflection can be tricky – skip indexed properties to prevent TargetParameterCountException. ? Recursion is key for deep object traversal and ensuring consistent data transformation.
?? Have you ever faced issues with reflection and collections in ASP.NET Core? Share your thoughts! ??
#dotnet #csharp #aspnetcore #reflection #coding