Example #1
0
        /// <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);
        }
Example #2
0
        public static AttributeExtCollection GetCustomAttriutesForMethod(MethodInfo member, bool inherit,
                                                                         Type attributeType)
        {
            AttributeExtCollection result;

            object[] attributes;
            if (attributeType == null)
            {
                attributes = member.GetCustomAttributes(inherit);
            }
            else
            {
                attributes = member.GetCustomAttributes(attributeType, inherit);
            }
            result = AttributeExtCollection.ConvertToAttributeCollection(attributes);
            if (inherit)
            {
                // check also for interface methods ...
                if (member.IsVirtual)
                {
                    // check also for attributes on interface method, if it's a implementation of an interface method
                    Type declaringType = member.DeclaringType;
                    // search interfaces for method definition
                    Type[] interfaces = declaringType.GetInterfaces();
                    foreach (Type interf in interfaces)
                    {
                        MethodInfo found = IsMethodDefinedInInterface(member, interf);
                        if (found != null)
                        {
                            // add attributes from interface definition if not already present
                            object[] inheritedAttributes;
                            if (attributeType == null)
                            {
                                inheritedAttributes = found.GetCustomAttributes(inherit);
                            }
                            else
                            {
                                inheritedAttributes = found.GetCustomAttributes(attributeType, inherit);
                            }
                            result = result.MergeMissingAttributes(inheritedAttributes);
                        }
                    }
                }
            }
            return(result);
        }
Example #3
0
        /// <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);
        }
Example #4
0
 public static AttributeExtCollection GetCustomAttriutesForField(FieldInfo member, bool inherit)
 {
     object[] attributes = member.GetCustomAttributes(inherit);
     return(AttributeExtCollection.ConvertToAttributeCollection(attributes));
 }
Example #5
0
 /// <summary>
 /// get the custom attributes for the Type type.
 /// </summary>
 /// <param name="type">the type to get the attributes for</param>
 /// <param name="inherit">should attributes on inherited type also be returned</param>
 /// <returns></returns>
 public static AttributeExtCollection GetCustomAttributesForType(Type type, bool inherit)
 {
     object[] attributes = type.GetCustomAttributes(inherit);
     return(AttributeExtCollection.ConvertToAttributeCollection(attributes));
 }