aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-07-26 22:57:02 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-07-26 22:57:02 -0500
commite4a7073ea750c6bf3a82b35b81ba3883f8438fb4 (patch)
tree4279f21105fc721b68c431f09afb1308b308014e /src
parent22e4e494b0baeb1983057f0b89dd03f97dfcdfc0 (diff)
downloadslskd-e4a7073ea750c6bf3a82b35b81ba3883f8438fb4.tar.gz
add GetPropertiesRecursively extension
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Extensions.cs24
1 files changed, 24 insertions, 0 deletions
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
@@ -45,6 +45,30 @@ namespace slskd
public static T ToObject<T>(this string str) => JsonSerializer.Deserialize<T>(str);
/// <summary>
+ /// Recursively retrieves all properties.
+ /// </summary>
+ /// <param name="type">The type from which to retrieve properties.</param>
+ /// <returns>The list of properties.</returns>
+ public static IEnumerable<PropertyInfo> GetPropertiesRecursively(this Type type)
+ {
+ var props = new List<PropertyInfo>();
+
+ 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;
+ }
+
+ /// <summary>
/// Deeply compares this object with the specified object and returns a list of properties that are different.
/// </summary>
/// <param name="left">The left side of the comparison.</param>