System.Attribute.InternalParamGetCustomAttributes C# (CSharp) Method

InternalParamGetCustomAttributes() private static method

private static InternalParamGetCustomAttributes ( MethodInfo method, ParameterInfo param, Type type, bool inherit ) : Attribute[]
method MethodInfo
param ParameterInfo
type Type
inherit bool
return Attribute[]
        private static Attribute[] InternalParamGetCustomAttributes(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 that
            // have this ParameterInfo defined. .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 MethodInfo's in the inheritance chain.
            // For MethodInfo's on an interface we do not do an inheritance walk so the default ParameterInfo attributes are returned.
            // For MethodInfo'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 and return the respective ParameterInfo attributes

            ArrayList disAllowMultiple = new ArrayList();
            Object [] objAttr;

            if (type == null)
                type = typeof(Attribute);

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

            // Get all the attributes that have Attribute as the base class
            Attribute [] ret = null;
            if (objAttr.Length == 0) 
                ret = (Attribute[])Array.CreateInstance(type,0);
            else 
                ret = (Attribute[])objAttr;
            
            if (method.DeclaringType == null) // This is an interface so we are done.
                return ret;
            
            if (!inherit) 
                return ret;
        
            int paramPosition = param.Position;
            method = method.GetParentDefinition();
            
            while (method != null)
            {
                // Find the ParameterInfo on this method
                ParameterInfo [] parameters = method.GetParameters();
                param = parameters[paramPosition]; // Point to the correct ParameterInfo of the method

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

                    if ((attribUsage.Inherited) && (disAllowMultiple.Contains(objType) == false))
                    {
                        if (attribUsage.AllowMultiple == false)
                            disAllowMultiple.Add(objType);
                        count++;
                    }
                    else
                        objAttr[i] = null;
                }

                // Get all the attributes that have Attribute as the base class
                Attribute [] attributes = (Attribute[])Array.CreateInstance(type,count);
                
                count = 0;
                for (int i =0;i < objAttr.Length;i++)
                {
                    if (objAttr[i] != null)
                    {
                        attributes[count] = (Attribute)objAttr[i];
                        count++;
                    }
                }
                
                Attribute [] temp = ret;
                ret = (Attribute[])Array.CreateInstance(type,temp.Length + count);
                Array.Copy(temp,ret,temp.Length);
                
                int offset = temp.Length;

                for (int i =0;i < attributes.Length;i++) 
                    ret[offset + i] = attributes[i];
    
                method = method.GetParentDefinition();
                
            } 

            return ret;
        
        }

Usage Example

Beispiel #1
0
 public static Attribute[] GetCustomAttributes(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"));
     }
     if (element.Member == (MemberInfo)null)
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_InvalidParameterInfo"), "element");
     }
     if (element.Member.MemberType == MemberTypes.Method && inherit)
     {
         return(Attribute.InternalParamGetCustomAttributes(element, attributeType, inherit));
     }
     else
     {
         return(element.GetCustomAttributes(attributeType, inherit) as Attribute[]);
     }
 }
All Usage Examples Of System.Attribute::InternalParamGetCustomAttributes