JamesNK/Newtonsoft.Json · error · Exception
Cannot get attributes from '{0}'.
Error message
Cannot get attributes from '{0}'. What it means
Thrown by ReflectionUtils.GetAttributes in the PORTABLE40 (legacy PCL) build when the attributeProvider object is not a Type, Assembly, MemberInfo, Module, or ParameterInfo. In the PORTABLE40 build there is no ICustomAttributeProvider fallback, so any unsupported provider hits this throw.
Source
Thrown at Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs:826
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
case Module m:
return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit);
#endif
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);
return (Attribute[])result;
#else
throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider));
#endif
}
}
#else
public static T[] GetAttributes<T>(object attributeProvider, bool inherit) where T : Attribute
{
return GetAttributes(attributeProvider, typeof(T), inherit).Cast<T>().ToArray();
}
public static Attribute[] GetAttributes(object provider, Type? attributeType, bool inherit)
{
switch (provider)
{
case Type t:
return (attributeType != null)
? t.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray()
: t.GetTypeInfo().GetCustomAttributes(inherit).ToArray();
case Assembly a:View on GitHub (pinned to 4f73e74372)
Solutions
- Pass a supported attribute provider (Type, MemberInfo, Assembly, Module, or ParameterInfo).
- Migrate off the Portable40 build to netstandard2.0, which has the ICustomAttributeProvider fallback path.
Example fix
// before var attrs = ReflectionUtils.GetAttributes(someRandomObject, null, false); // after var attrs = ReflectionUtils.GetAttributes(typeof(Foo), null, false);
Defensive patterns
Strategy: type-guard
Validate before calling
if (provider is not Type and not MemberInfo and not Assembly and not Module and not ParameterInfo)
throw new ArgumentException($"Unsupported attribute provider: {provider?.GetType()}"); Type guard
static bool IsAttributeProvider(object o) => o is Type or MemberInfo or Assembly or Module or ParameterInfo;
Prevention
- Pass only supported reflection provider types to GetAttributes.
- Migrate off the Portable40 build to netstandard2.0.
- Validate the provider type before invoking the attribute helper.
When it happens
Trigger: Passing an arbitrary object (not one of the supported reflection providers) to GetAttributes while running the Portable40 build of Json.NET. The other builds fall back to ICustomAttributeProvider and only throw elsewhere (see error 278).
Common situations: Legacy PCL projects consuming the Portable40 build, custom attribute-reading helpers passing boxed values, or migrating from an old portable class library.
Related errors
- type
- Unexpected type code '{0}' for type '{1}'.
- Error setting value to '{0}' on '{1}'.
- Error getting value from '{0}' on '{1}'.
- Error setting value to '{0}' on '{1}'.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/7dd59f07f6089af1.
Report an issue: GitHub.