From 93fe3bd618f2e9b232d1cec7ac4fe35c3f74adf5 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 1 Jul 2018 15:25:36 +0800 Subject: [PATCH] ReflectionUtils code clean up (#1755) --- .../Utilities/ReflectionUtils.cs | 104 +++++++----------- 1 file changed, 38 insertions(+), 66 deletions(-) diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index 77b9aa99..c86995f5 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -743,8 +743,7 @@ namespace Newtonsoft.Json.Utilities { Attribute[] a = GetAttributes(attributeProvider, typeof(T), inherit); - T[] attributes = a as T[]; - if (attributes != null) + if (a is T[] attributes) { return attributes; } @@ -761,57 +760,41 @@ namespace Newtonsoft.Json.Utilities // http://hyperthink.net/blog/getcustomattributes-gotcha/ // ICustomAttributeProvider doesn't do inheritance - Type t = provider as Type; - if (t != null) + switch (provider) { - object[] array = attributeType != null ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit); - Attribute[] attributes = array.Cast().ToArray(); + case Type t: + object[] array = attributeType != null ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit); + Attribute[] attributes = array.Cast().ToArray(); #if (NET20 || NET35) - // ye olde .NET GetCustomAttributes doesn't respect the inherit argument - if (inherit && t.BaseType != null) - { - attributes = attributes.Union(GetAttributes(t.BaseType, attributeType, inherit)).ToArray(); - } + // ye olde .NET GetCustomAttributes doesn't respect the inherit argument + if (inherit && t.BaseType != null) + { + attributes = attributes.Union(GetAttributes(t.BaseType, attributeType, inherit)).ToArray(); + } #endif - return attributes; - } - - Assembly a = provider as Assembly; - if (a != null) - { - return (attributeType != null) ? Attribute.GetCustomAttributes(a, attributeType) : Attribute.GetCustomAttributes(a); - } - - MemberInfo mi = provider as MemberInfo; - if (mi != null) - { - return (attributeType != null) ? Attribute.GetCustomAttributes(mi, attributeType, inherit) : Attribute.GetCustomAttributes(mi, inherit); - } - + return attributes; + case Assembly a: + return (attributeType != null) ? Attribute.GetCustomAttributes(a, attributeType) : Attribute.GetCustomAttributes(a); + case MemberInfo mi: + return (attributeType != null) ? Attribute.GetCustomAttributes(mi, attributeType, inherit) : Attribute.GetCustomAttributes(mi, inherit); #if !PORTABLE40 - Module m = provider as Module; - if (m != null) - { - return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit); - } + case Module m: + return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit); #endif - - ParameterInfo p = provider as ParameterInfo; - if (p != null) - { - return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit); - } - + case ParameterInfo p: + return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit); + default: #if !PORTABLE40 - ICustomAttributeProvider customAttributeProvider = (ICustomAttributeProvider)attributeProvider; - object[] result = (attributeType != null) ? customAttributeProvider.GetCustomAttributes(attributeType, inherit) : customAttributeProvider.GetCustomAttributes(inherit); + ICustomAttributeProvider customAttributeProvider = (ICustomAttributeProvider)attributeProvider; + object[] result = (attributeType != null) ? customAttributeProvider.GetCustomAttributes(attributeType, inherit) : customAttributeProvider.GetCustomAttributes(inherit); - return (Attribute[])result; + return (Attribute[])result; #else - throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider)); + throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider)); #endif + } } #else public static T[] GetAttributes(object attributeProvider, bool inherit) where T : Attribute @@ -821,31 +804,20 @@ namespace Newtonsoft.Json.Utilities public static Attribute[] GetAttributes(object provider, Type attributeType, bool inherit) { - if (provider is Type t) + switch (provider) { - return (attributeType != null) - ? t.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray() - : t.GetTypeInfo().GetCustomAttributes(inherit).ToArray(); - } - - if (provider is Assembly a) - { - return (attributeType != null) ? a.GetCustomAttributes(attributeType).ToArray() : a.GetCustomAttributes().ToArray(); - } - - if (provider is MemberInfo memberInfo) - { - return (attributeType != null) ? memberInfo.GetCustomAttributes(attributeType, inherit).ToArray() : memberInfo.GetCustomAttributes(inherit).ToArray(); - } - - if (provider is Module module) - { - return (attributeType != null) ? module.GetCustomAttributes(attributeType).ToArray() : module.GetCustomAttributes().ToArray(); - } - - if (provider is ParameterInfo parameterInfo) - { - return (attributeType != null) ? parameterInfo.GetCustomAttributes(attributeType, inherit).ToArray() : parameterInfo.GetCustomAttributes(inherit).ToArray(); + case Type t: + return (attributeType != null) + ? t.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray() + : t.GetTypeInfo().GetCustomAttributes(inherit).ToArray(); + case Assembly a: + return (attributeType != null) ? a.GetCustomAttributes(attributeType).ToArray() : a.GetCustomAttributes().ToArray(); + case MemberInfo memberInfo: + return (attributeType != null) ? memberInfo.GetCustomAttributes(attributeType, inherit).ToArray() : memberInfo.GetCustomAttributes(inherit).ToArray(); + case Module module: + return (attributeType != null) ? module.GetCustomAttributes(attributeType).ToArray() : module.GetCustomAttributes().ToArray(); + case ParameterInfo parameterInfo: + return (attributeType != null) ? parameterInfo.GetCustomAttributes(attributeType, inherit).ToArray() : parameterInfo.GetCustomAttributes(inherit).ToArray(); } throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider));