From e4a7073ea750c6bf3a82b35b81ba3883f8438fb4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 26 Jul 2021 22:57:02 -0500 Subject: add GetPropertiesRecursively extension --- src/slskd/Common/Extensions.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 3e4f6e68..33faecf7 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -44,6 +44,30 @@ namespace slskd /// The new object deserialzied from the string. public static T ToObject(this string str) => JsonSerializer.Deserialize(str); + /// + /// Recursively retrieves all properties. + /// + /// The type from which to retrieve properties. + /// The list of properties. + public static IEnumerable GetPropertiesRecursively(this Type type) + { + var props = new List(); + + foreach (var prop in type.GetProperties()) + { + if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsArray || Nullable.GetUnderlyingType(prop.PropertyType) != null || new[] { typeof(string), typeof(decimal) }.Contains(prop.PropertyType)) + { + props.Add(prop); + } + else + { + props.AddRange(prop.PropertyType.GetPropertiesRecursively()); + } + } + + return props; + } + /// /// Deeply compares this object with the specified object and returns a list of properties that are different. /// -- cgit v1.2.3