System.Reflection.CustomAttribute.GetCustomAttributes C# (CSharp) Method

GetCustomAttributes() static private method

static private GetCustomAttributes ( RuntimeMethodInfo method, RuntimeType caType, bool inherit ) : Object[]
method RuntimeMethodInfo
caType RuntimeType
inherit bool
return Object[]
        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;
        }

Same methods

CustomAttribute::GetCustomAttributes ( Assembly assembly, RuntimeType caType ) : Object[]
CustomAttribute::GetCustomAttributes ( Module module, RuntimeType caType ) : Object[]
CustomAttribute::GetCustomAttributes ( ParameterInfo parameter, RuntimeType caType ) : Object[]
CustomAttribute::GetCustomAttributes ( RuntimeConstructorInfo ctor, RuntimeType caType ) : Object[]
CustomAttribute::GetCustomAttributes ( RuntimeEventInfo e, RuntimeType caType ) : Object[]
CustomAttribute::GetCustomAttributes ( RuntimeFieldInfo field, RuntimeType caType ) : Object[]
CustomAttribute::GetCustomAttributes ( RuntimePropertyInfo property, RuntimeType caType ) : Object[]
CustomAttribute::GetCustomAttributes ( RuntimeType type, RuntimeType caType, bool inherit ) : Object[]
CustomAttribute::GetCustomAttributes ( Module decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType ) : object[]
CustomAttribute::GetCustomAttributes ( Module decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes ) : object[]

Usage Example

Exemplo n.º 1
0
 public override Object[] GetCustomAttributes(bool inherit)
 {
     return(CustomAttribute.GetCustomAttributes(this, typeof(object) as RuntimeType));
 }
All Usage Examples Of System.Reflection.CustomAttribute::GetCustomAttributes