{"record":{"id":"7dd59f07f6089af1","repo":"JamesNK/Newtonsoft.Json","slug":"cannot-get-attributes-from-0","errorCode":null,"errorMessage":"Cannot get attributes from '{0}'.","messagePattern":"Cannot get attributes from '(.+?)'\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs","lineNumber":826,"sourceCode":"                    return attributes;\n                case Assembly a:\n                    return (attributeType != null) ? Attribute.GetCustomAttributes(a, attributeType) : Attribute.GetCustomAttributes(a);\n                case MemberInfo mi:\n                    return (attributeType != null) ? Attribute.GetCustomAttributes(mi, attributeType, inherit) : Attribute.GetCustomAttributes(mi, inherit);\n#if !PORTABLE40\n                case Module m:\n                    return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit);\n#endif\n                case ParameterInfo p:\n                    return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit);\n                default:\n#if !PORTABLE40\n                    ICustomAttributeProvider customAttributeProvider = (ICustomAttributeProvider)attributeProvider;\n                    object[] result = (attributeType != null) ? customAttributeProvider.GetCustomAttributes(attributeType, inherit) : customAttributeProvider.GetCustomAttributes(inherit);\n\n                    return (Attribute[])result;\n#else\n                    throw new Exception(\"Cannot get attributes from '{0}'.\".FormatWith(CultureInfo.InvariantCulture, provider));\n#endif\n            }\n        }\n#else\n        public static T[] GetAttributes<T>(object attributeProvider, bool inherit) where T : Attribute\n        {\n            return GetAttributes(attributeProvider, typeof(T), inherit).Cast<T>().ToArray();\n        }\n\n        public static Attribute[] GetAttributes(object provider, Type? attributeType, bool inherit)\n        {\n            switch (provider)\n            {\n                case Type t:\n                    return (attributeType != null)\n                        ? t.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray()\n                        : t.GetTypeInfo().GetCustomAttributes(inherit).ToArray();\n                case Assembly a:","sourceCodeStart":808,"sourceCodeEnd":844,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs#L808-L844","documentation":"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.","triggerScenarios":"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).","commonSituations":"Legacy PCL projects consuming the Portable40 build, custom attribute-reading helpers passing boxed values, or migrating from an old portable class library.","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."],"exampleFix":"// before\nvar attrs = ReflectionUtils.GetAttributes(someRandomObject, null, false);\n\n// after\nvar attrs = ReflectionUtils.GetAttributes(typeof(Foo), null, false);","handlingStrategy":"type-guard","validationCode":"if (provider is not Type and not MemberInfo and not Assembly and not Module and not ParameterInfo)\n    throw new ArgumentException($\"Unsupported attribute provider: {provider?.GetType()}\");","typeGuard":"static bool IsAttributeProvider(object o) => o is Type or MemberInfo or Assembly or Module or ParameterInfo;","tryCatchPattern":null,"preventionTips":["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."],"tags":["reflection","attributes","portable40","legacy","pcl"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}