/// <summary> /// collects the custom attributes on the return parameter and from /// the return paramters from inherited methods. /// </summary> /// <returns>a collection of attributes</returns> public static AttributeExtCollection CollectReturnParameterAttributes(MethodInfo method) { AttributeExtCollection result = AttributeExtCollection.ConvertToAttributeCollection( method.ReturnTypeCustomAttributes.GetCustomAttributes(true)); if (!method.IsVirtual) { return(result); } MethodInfo baseDecl = method.GetBaseDefinition(); if (!baseDecl.Equals(method)) { // add return param attributes from base definition if not already present result = result.MergeMissingAttributes(baseDecl.ReturnTypeCustomAttributes.GetCustomAttributes(true)); } Type declaringType = method.DeclaringType; // search interfaces for method definition Type[] interfaces = declaringType.GetInterfaces(); foreach (Type interf in interfaces) { MethodInfo found = IsMethodDefinedInInterface(method, interf); if (found != null) { // add return param attributes from interface definition if not already present result = result.MergeMissingAttributes(found.ReturnTypeCustomAttributes.GetCustomAttributes(true)); } } return(result); }
/// <summary> /// collects the custom attributes on the current parameter and from /// the paramters from inherited methods. /// </summary> /// <param name="paramInfo">the parameter to check</param> /// <returns>a collection of attributes</returns> public static AttributeExtCollection CollectParameterAttributes(ParameterInfo paramInfo, MethodInfo paramInMethod) { AttributeExtCollection result = AttributeExtCollection.ConvertToAttributeCollection( paramInfo.GetCustomAttributes(true)); if (!paramInMethod.IsVirtual) { return(result); } MethodInfo baseDecl = paramInMethod.GetBaseDefinition(); if (!baseDecl.Equals(paramInMethod)) { // add param attributes from base definition if not already present ParameterInfo[] baseParams = baseDecl.GetParameters(); ParameterInfo baseParamToConsider = baseParams[paramInfo.Position]; result = result.MergeMissingAttributes(baseParamToConsider.GetCustomAttributes(true)); } Type declaringType = paramInMethod.DeclaringType; // search interfaces for method definition Type[] interfaces = declaringType.GetInterfaces(); foreach (Type interf in interfaces) { MethodInfo found = IsMethodDefinedInInterface(paramInMethod, interf); if (found != null) { // add param attributes from interface definition if not already present ParameterInfo[] ifParams = found.GetParameters(); ParameterInfo ifParamToConsider = ifParams[paramInfo.Position]; result = result.MergeMissingAttributes(ifParamToConsider.GetCustomAttributes(true)); } } return(result); }