System.Attribute.InternalParamIsDefined C# (CSharp) Method

InternalParamIsDefined() private static method

private static InternalParamIsDefined ( MethodInfo method, ParameterInfo param, Type type, bool inherit ) : bool
method MethodInfo
param ParameterInfo
type Type
inherit bool
return bool
        private static bool InternalParamIsDefined(MethodInfo method, ParameterInfo param, Type type, bool inherit)
        {
            // For ParameterInfo's we need to make sure that we chain through all the MethodInfo's in the inheritance chain.
            // We pick up all the CustomAttributes for the starting ParameterInfo. We need to pick up only attributes 
            // that are marked inherited from the remainder of the ParameterInfo's in the inheritance chain.
            // For MethodInfo's on an interface we do not do an inheritance walk. For ParameterInfo's on a
            // Class we walk up the inheritance chain but do not look at the MethodInfo's on the interfaces that the class inherits from.

            if (param.IsDefined(type, false))
                return true;
            
            if (method.DeclaringType == null || !inherit) // This is an interface so we are done.
                return false;
        
            int paramPosition = param.Position;
            method = method.GetParentDefinition();
                        
            while (method != null)
            {
                ParameterInfo [] parameters = method.GetParameters();
                param = parameters[paramPosition]; 

                Object [] objAttr = param.GetCustomAttributes(type, false); 
                                
                for (int i =0; i < objAttr.Length; i++)
                {
                    Type objType = objAttr[i].GetType();
                    AttributeUsageAttribute attribUsage = InternalGetAttributeUsage(objType);

                    if ((objAttr[i] is Attribute) && (attribUsage.Inherited))
                        return true;
                }
                    
                method = method.GetParentDefinition();                
            } 

            return false;
        }

Usage Example

Beispiel #1
0
        public static bool IsDefined(ParameterInfo element, Type attributeType, bool inherit)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            if (attributeType == (Type)null)
            {
                throw new ArgumentNullException("attributeType");
            }
            if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass"));
            }
            switch (element.Member.MemberType)
            {
            case MemberTypes.Constructor:
                return(element.IsDefined(attributeType, false));

            case MemberTypes.Method:
                return(Attribute.InternalParamIsDefined(element, attributeType, inherit));

            case MemberTypes.Property:
                return(element.IsDefined(attributeType, false));

            default:
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidParamInfo"));
            }
        }