internal static Object[] GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, bool inherit)
{
if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
method = method.GetGenericMethodDefinition() as RuntimeMethodInfo;
int pcaCount = 0;
Attribute[] pca = PseudoCustomAttribute.GetCustomAttributes(method, caType, true, out pcaCount);
// if we are asked to go up the hierarchy chain we have to do it now and regardless of the
// attribute usage for the specific attribute because a derived attribute may override the usage...
// ... however if the attribute is sealed we can rely on the attribute usage
if (!inherit || (caType.IsSealed && !CustomAttribute.GetAttributeUsage(caType).Inherited))
{
object[] attributes = GetCustomAttributes(method.Module, method.MetadataToken, pcaCount, caType);
if (pcaCount > 0) Array.Copy(pca, 0, attributes, attributes.Length - pcaCount, pcaCount);
return attributes;
}
List<object> result = new List<object>();
bool mustBeInheritable = false;
bool useObjectArray = (caType == null || caType.IsValueType || caType.ContainsGenericParameters);
Type arrayType = useObjectArray ? typeof(object) : caType;
while (pcaCount > 0)
result.Add(pca[--pcaCount]);
while (method != null)
{
object[] attributes = GetCustomAttributes(method.Module, method.MetadataToken, 0, caType, mustBeInheritable, result);
mustBeInheritable = true;
for (int i = 0; i < attributes.Length; i++)
result.Add(attributes[i]);
method = method.GetParentDefinition() as RuntimeMethodInfo;
}
object[] typedResult = Array.CreateInstance(arrayType, result.Count) as object[];
Array.Copy(result.ToArray(), 0, typedResult, 0, result.Count);
return typedResult;
}