using System.Text; namespace adas_core.Domain.Utils; /// /// Provides static utility methods for common object-related operations. /// public static class ObjectUtils { /// /// Converts an object to a string representation showing its type name and non-null property values in the format "TypeName[Property1: Value1,Property2: Value2]". /// Returns "[OBJ NULL]" when the object is null, and skips properties whose values are null. /// /// The object to convert to a string. Can be null. /// A string containing the type name followed by the object's non-null property values and their names. public static string ToStr(this object? obj) { if (obj == null) return "[OBJ NULL]"; var type = obj.GetType(); //if (type == null) return "[OBJ TYPE NULL]"; StringBuilder sb = new(); sb.Append(type.Name + "["); foreach (var property in obj.GetType().GetProperties()) { var value = obj.GetType().GetProperty(property.Name)?.GetValue(obj); if (value == null) continue; sb.Append($"{property.Name}: {value},"); } sb.Append(']'); return sb.ToString(); } }