public override object[] GetCustomAttributes(Type attributeType, bool inherit)
            {
                List <object> result = new List <object>();

                // Then add all the attributes from the attribute table.
                result.AddRange(ReflectionContext.Table.GetCustomAttributes(this).Where(attr => attributeType.IsAssignableFrom(attr.GetType())));

                // Then check this type, without inheritance. (Parameter attributes are not inherited). Add only attributes if Multiple = true OR attribute not already exists.
                foreach (var ca in base.GetCustomAttributes(attributeType, false))
                {
                    if (AttributeUtil.GetAttributeUsage(ca.GetType()).AllowMultiple || !result.Any(attr => attr.GetType().Equals(ca.GetType())))
                    {
                        result.Add(ca);
                    }
                }

                // Finally create a resulting array of the correct type.
                object[] arrResult = (object[])Array.CreateInstance(attributeType, result.Count);
                for (int i = 0; i < result.Count; i++)
                {
                    arrResult[i] = result[i];
                }

                return(arrResult);
            }
Example #2
0
            public override object[] GetCustomAttributes(Type attributeType, bool inherit)
            {
                List <object> result = new List <object>();

                // Then add any attributes defined in the reflection context table.
                result.AddRange(ReflectionContext.Table.GetCustomAttributes(this).Where(attr => attributeType.IsAssignableFrom(attr.GetType())));

                // Then check this method, without inheritance. Add only attributes if Multiple = true OR attribute not already exists.
                foreach (var ca in base.GetCustomAttributes(attributeType, false))
                {
                    if (AttributeUtil.GetAttributeUsage(ca.GetType()).AllowMultiple || !result.Any(attr => attr.GetType().Equals(ca.GetType())))
                    {
                        result.Add(ca);
                    }
                }

                // If we want also inherited attributes, we must check the base definition.
                if (inherit && (ReflectionContext.Options & TableReflectionContextOptions.HonorEventAttributeInheritance) != 0)
                {
                    // ...if it is different from the declaring type of this method, we get all attributes from there and add them as well, depending
                    // on their attribute usage settings.
                    MemberInfo baseEvent = this.GetParentDefinition();

                    // Then get base attributes, add only if Inherit = true AND (Multiple = true OR attribute not already exists).
                    if (baseEvent != null && !baseEvent.DeclaringType.Equals(DeclaringType))
                    {
                        foreach (var ca in baseEvent.GetCustomAttributes(attributeType, true))
                        {
                            AttributeUsageAttribute attributeUsage = AttributeUtil.GetAttributeUsage(ca.GetType());
                            if (attributeUsage.Inherited && (attributeUsage.AllowMultiple || !result.Any(attr => attr.GetType().Equals(ca.GetType()))))
                            {
                                result.Add(ca);
                            }
                        }
                    }
                }

                object[] arrResult = (object[])Array.CreateInstance(attributeType, result.Count);
                for (int i = 0; i < result.Count; i++)
                {
                    arrResult[i] = result[i];
                }

                return(arrResult);
            }
            public override object[] GetCustomAttributes(Type attributeType, bool inherit)
            {
                bool stop = this.IsGenericType;
                // Start with the table attributes.
                List <object> result = new List <object>();

                result.AddRange(ReflectionContext.Table.GetCustomAttributes(this).Where(attr => attributeType.IsAssignableFrom(attr.GetType())));

                // Then check this type, without inheritance. Add only attributes if Multiple = true OR attribute not already exists.
                foreach (var ca in base.GetCustomAttributes(attributeType, false))
                {
                    if (AttributeUtil.GetAttributeUsage(ca.GetType()).AllowMultiple || !result.Any(attr => attr.GetType().Equals(ca.GetType())))
                    {
                        result.Add(ca);
                    }
                }

                // Then get base attributes, add only if Inherit = true AND (Multiple = true OR attribute not already exists).
                if (inherit && BaseType != null && !BaseType.Equals(typeof(object)))
                {
                    foreach (var ca in BaseType.GetCustomAttributes(attributeType, inherit))
                    {
                        AttributeUsageAttribute attributeUsage = AttributeUtil.GetAttributeUsage(ca.GetType());
                        if (attributeUsage.Inherited && (attributeUsage.AllowMultiple || !result.Any(attr => attr.GetType().Equals(ca.GetType()))))
                        {
                            result.Add(ca);
                        }
                    }
                }

                object[] arrResult = (object[])Array.CreateInstance(attributeType, result.Count);
                for (int i = 0; i < result.Count; i++)
                {
                    arrResult[i] = result[i];
                }

                return(arrResult);
            }