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,9 +760,9 @@ 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)
{ {
case Type t:
object[] array = attributeType != null ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit); object[] array = attributeType != null ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit);
Attribute[] attributes = array.Cast<Attribute>().ToArray(); Attribute[] attributes = array.Cast<Attribute>().ToArray();
@@ -776,34 +775,17 @@ namespace Newtonsoft.Json.Utilities
#endif #endif
return attributes; return attributes;
} case Assembly a:
Assembly a = provider as Assembly;
if (a != null)
{
return (attributeType != null) ? Attribute.GetCustomAttributes(a, attributeType) : Attribute.GetCustomAttributes(a); return (attributeType != null) ? Attribute.GetCustomAttributes(a, attributeType) : Attribute.GetCustomAttributes(a);
} case MemberInfo mi:
MemberInfo mi = provider as MemberInfo;
if (mi != null)
{
return (attributeType != null) ? Attribute.GetCustomAttributes(mi, attributeType, inherit) : Attribute.GetCustomAttributes(mi, inherit); 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;
if (p != null)
{
return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit); return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit);
} default:
#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);
@@ -813,6 +795,7 @@ namespace Newtonsoft.Json.Utilities
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,30 +804,19 @@ 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)
{ {
case Type t:
return (attributeType != null) return (attributeType != null)
? t.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray() ? t.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray()
: t.GetTypeInfo().GetCustomAttributes(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(); return (attributeType != null) ? a.GetCustomAttributes(attributeType).ToArray() : a.GetCustomAttributes().ToArray();
} case MemberInfo memberInfo:
if (provider is MemberInfo memberInfo)
{
return (attributeType != null) ? memberInfo.GetCustomAttributes(attributeType, inherit).ToArray() : memberInfo.GetCustomAttributes(inherit).ToArray(); return (attributeType != null) ? memberInfo.GetCustomAttributes(attributeType, inherit).ToArray() : memberInfo.GetCustomAttributes(inherit).ToArray();
} case Module module:
if (provider is Module module)
{
return (attributeType != null) ? module.GetCustomAttributes(attributeType).ToArray() : module.GetCustomAttributes().ToArray(); return (attributeType != null) ? module.GetCustomAttributes(attributeType).ToArray() : module.GetCustomAttributes().ToArray();
} case ParameterInfo parameterInfo:
if (provider is ParameterInfo parameterInfo)
{
return (attributeType != null) ? parameterInfo.GetCustomAttributes(attributeType, inherit).ToArray() : parameterInfo.GetCustomAttributes(inherit).ToArray(); return (attributeType != null) ? parameterInfo.GetCustomAttributes(attributeType, inherit).ToArray() : parameterInfo.GetCustomAttributes(inherit).ToArray();
} }