ReflectionUtils code clean up (#1755)

This commit is contained in:
James Newton-King
2018-07-01 15:25:36 +08:00
committed by GitHub
parent aa954a93f5
commit 93fe3bd618
@@ -743,8 +743,7 @@ namespace Newtonsoft.Json.Utilities
{ {
Attribute[] a = GetAttributes(attributeProvider, typeof(T), inherit); Attribute[] a = GetAttributes(attributeProvider, typeof(T), inherit);
T[] attributes = a as T[]; if (a is T[] attributes)
if (attributes != null)
{ {
return attributes; return attributes;
} }
@@ -761,57 +760,41 @@ namespace Newtonsoft.Json.Utilities
// http://hyperthink.net/blog/getcustomattributes-gotcha/ // http://hyperthink.net/blog/getcustomattributes-gotcha/
// ICustomAttributeProvider doesn't do inheritance // ICustomAttributeProvider doesn't do inheritance
Type t = provider as Type; switch (provider)
if (t != null)
{ {
object[] array = attributeType != null ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit); case Type t:
Attribute[] attributes = array.Cast<Attribute>().ToArray(); object[] array = attributeType != null ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit);
Attribute[] attributes = array.Cast<Attribute>().ToArray();
#if (NET20 || NET35) #if (NET20 || NET35)
// ye olde .NET GetCustomAttributes doesn't respect the inherit argument // ye olde .NET GetCustomAttributes doesn't respect the inherit argument
if (inherit && t.BaseType != null) if (inherit && t.BaseType != null)
{ {
attributes = attributes.Union(GetAttributes(t.BaseType, attributeType, inherit)).ToArray(); attributes = attributes.Union(GetAttributes(t.BaseType, attributeType, inherit)).ToArray();
} }
#endif #endif
return attributes; return attributes;
} case Assembly a:
return (attributeType != null) ? Attribute.GetCustomAttributes(a, attributeType) : Attribute.GetCustomAttributes(a);
Assembly a = provider as Assembly; case MemberInfo mi:
if (a != null) return (attributeType != null) ? Attribute.GetCustomAttributes(mi, attributeType, inherit) : Attribute.GetCustomAttributes(mi, inherit);
{
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);
}
#if !PORTABLE40 #if !PORTABLE40
Module m = provider as Module; case Module m:
if (m != null) return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit);
{
return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit);
}
#endif #endif
case ParameterInfo p:
ParameterInfo p = provider as ParameterInfo; return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit);
if (p != null) default:
{
return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit);
}
#if !PORTABLE40 #if !PORTABLE40
ICustomAttributeProvider customAttributeProvider = (ICustomAttributeProvider)attributeProvider; ICustomAttributeProvider customAttributeProvider = (ICustomAttributeProvider)attributeProvider;
object[] result = (attributeType != null) ? customAttributeProvider.GetCustomAttributes(attributeType, inherit) : customAttributeProvider.GetCustomAttributes(inherit); object[] result = (attributeType != null) ? customAttributeProvider.GetCustomAttributes(attributeType, inherit) : customAttributeProvider.GetCustomAttributes(inherit);
return (Attribute[])result; return (Attribute[])result;
#else #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 #endif
}
} }
#else #else
public static T[] GetAttributes<T>(object attributeProvider, bool inherit) where T : Attribute public static T[] GetAttributes<T>(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) public static Attribute[] GetAttributes(object provider, Type attributeType, bool inherit)
{ {
if (provider is Type t) switch (provider)
{ {
return (attributeType != null) case Type t:
? t.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray() return (attributeType != null)
: t.GetTypeInfo().GetCustomAttributes(inherit).ToArray(); ? t.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray()
} : t.GetTypeInfo().GetCustomAttributes(inherit).ToArray();
case Assembly a:
if (provider is Assembly a) return (attributeType != null) ? a.GetCustomAttributes(attributeType).ToArray() : a.GetCustomAttributes().ToArray();
{ case MemberInfo memberInfo:
return (attributeType != null) ? a.GetCustomAttributes(attributeType).ToArray() : a.GetCustomAttributes().ToArray(); 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();
if (provider is MemberInfo memberInfo) case ParameterInfo parameterInfo:
{ return (attributeType != null) ? parameterInfo.GetCustomAttributes(attributeType, inherit).ToArray() : parameterInfo.GetCustomAttributes(inherit).ToArray();
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();
} }
throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider)); throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider));