Why does ICustomAttributeProvider.GetCustomAttributes()
return object[]
instead of Attribute[]
?
Is there any circumstance when using the ICustomAttributeProvider
implementations from mscorlib and System assemblies will return objects that are not of type Attribute
?
The CLI spec (ECMA 335) Partition II, Clause 21 states in part:
In other words, a language that is not CLS-compliant may allow you to specify attributes that do not derive from
Attribute
, so theGetCustomAttributes
method is probably designed to allow such attributes to be consumed.I'm pretty sure that no such non-CLS-compliant language exists, and .NET doesn't support it, but one can imagine that the designers of Reflection did not want to preclude the possibility in the future.
As for your second question, a quick inspection of the source code for
System.Reflection
shows that you always get anAttribute[]
back. Since the returned objects are alwaysAttribute[]
, you can safely cast them, but there's no guarantee that it will always work that way.