From 47c70b89be1a13b321e2726e2f4b05af56c67e67 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 9 Apr 2022 12:30:18 -0500 Subject: swallow exceptions raised when trying to traverse primitive types --- src/slskd/Program.cs | 92 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 0857ef4d..bcf1df0a 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) slskd Team. All rights reserved. // // This program is free software: you can redistribute it and/or modify @@ -468,31 +468,38 @@ namespace slskd void Map(Type type) { - var defaults = Activator.CreateInstance(type); - var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - - foreach (PropertyInfo property in props) + try { - var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(ArgumentAttribute)); - var descriptionAttribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(DescriptionAttribute)); - var isRequired = property.CustomAttributes.Any(a => a.AttributeType == typeof(RequiredAttribute)); + var defaults = Activator.CreateInstance(type); + var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - if (attribute != default) + foreach (PropertyInfo property in props) { - var shortName = (char)attribute.ConstructorArguments[0].Value; - var longName = (string)attribute.ConstructorArguments[1].Value; - var description = descriptionAttribute?.ConstructorArguments[0].Value; - - var suffix = isRequired ? " (required)" : $" (default: {property.GetValue(defaults) ?? ""})"; - var item = $"{(shortName == default ? " " : $"{shortName}|")}--{GetLongName(longName, property.PropertyType)}"; - var desc = $"{description}{(property.PropertyType == typeof(bool) ? string.Empty : suffix)}"; - lines.Add(new(item, desc)); - } - else - { - Map(property.PropertyType); + var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(ArgumentAttribute)); + var descriptionAttribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(DescriptionAttribute)); + var isRequired = property.CustomAttributes.Any(a => a.AttributeType == typeof(RequiredAttribute)); + + if (attribute != default) + { + var shortName = (char)attribute.ConstructorArguments[0].Value; + var longName = (string)attribute.ConstructorArguments[1].Value; + var description = descriptionAttribute?.ConstructorArguments[0].Value; + + var suffix = isRequired ? " (required)" : $" (default: {property.GetValue(defaults) ?? ""})"; + var item = $"{(shortName == default ? " " : $"{shortName}|")}--{GetLongName(longName, property.PropertyType)}"; + var desc = $"{description}{(property.PropertyType == typeof(bool) ? string.Empty : suffix)}"; + lines.Add(new(item, desc)); + } + else + { + Map(property.PropertyType); + } } } + catch + { + return; + } } Map(targetType); @@ -516,30 +523,37 @@ namespace slskd void Map(Type type) { - var defaults = Activator.CreateInstance(type); - var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - - foreach (PropertyInfo property in props) + try { - var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(EnvironmentVariableAttribute)); - var descriptionAttribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(DescriptionAttribute)); - var isRequired = property.CustomAttributes.Any(a => a.AttributeType == typeof(RequiredAttribute)); + var defaults = Activator.CreateInstance(type); + var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); - if (attribute != default) + foreach (PropertyInfo property in props) { - var name = (string)attribute.ConstructorArguments[0].Value; - var description = descriptionAttribute?.ConstructorArguments[0].Value; + var attribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(EnvironmentVariableAttribute)); + var descriptionAttribute = property.CustomAttributes.FirstOrDefault(a => a.AttributeType == typeof(DescriptionAttribute)); + var isRequired = property.CustomAttributes.Any(a => a.AttributeType == typeof(RequiredAttribute)); - var suffix = isRequired ? " (required)" : $" (default: {property.GetValue(defaults) ?? ""})"; - var item = $"{prefix}{GetName(name, property.PropertyType)}"; - var desc = $"{description}{(type == typeof(bool) ? string.Empty : suffix)}"; - lines.Add(new(item, desc)); - } - else - { - Map(property.PropertyType); + if (attribute != default) + { + var name = (string)attribute.ConstructorArguments[0].Value; + var description = descriptionAttribute?.ConstructorArguments[0].Value; + + var suffix = isRequired ? " (required)" : $" (default: {property.GetValue(defaults) ?? ""})"; + var item = $"{prefix}{GetName(name, property.PropertyType)}"; + var desc = $"{description}{(type == typeof(bool) ? string.Empty : suffix)}"; + lines.Add(new(item, desc)); + } + else + { + Map(property.PropertyType); + } } } + catch + { + return; + } } Map(targetType); -- cgit v1.2.3