Implementations for Handling Empty Guid Values in Objects and Lists

Implementations for Handling Empty Guid Values in Objects and Lists

Implementations for Handling Empty Guid Values in Objects and Lists

  1. Basic Reflection Approach (Issue Present)
  2. Enhanced Reflection Approach (Fixes Guid and Lists)
  3. Optimized Reflection with Generic Type Checking


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?

  • ? Does not handle Guid (non-nullable).
  • ? Fails on List<T> properties.
  • ? Nested objects remain unchanged.


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:

  • ?? Uses IEnumerable<object>, causing boxing for value types in collections.
  • ?? Does not modify lists in place, only processes references.


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?

  • ? Basic Reflection ApproachNOT recommended (fails on lists, lacks deep object traversal).
  • ?? Enhanced Reflection ApproachGood but has performance issues due to boxing.
  • ? Optimized Reflection with IList Support → Best for handling List<T> efficiently and modifying in place.


?? 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

要查看或添加评论,请登录

Osama Nasir的更多文章

社区洞察