System.Attribute.IsDefined C# (CSharp) Method

IsDefined() public static method

public static IsDefined ( ParameterInfo element, Type attributeType, bool inherit ) : bool
element ParameterInfo
attributeType Type
inherit bool
return bool
        public static bool IsDefined(ParameterInfo element, Type attributeType, bool inherit)
        {
            // Returns true is a custom attribute subclass of attributeType class/interface with inheritance walk
            if (element == null)
                throw new ArgumentNullException("element");

            if (attributeType == null)
                throw new ArgumentNullException("attributeType");
            
            if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
                throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass"));

            MemberInfo member = element.Member;

            switch(member.MemberType)
            {
                case MemberTypes.Method: // We need to climb up the member hierarchy            
                    return InternalParamIsDefined((MethodInfo)member, element, attributeType, inherit);

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

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

                default: 
                    BCLDebug.Assert(false, "Invalid type for ParameterInfo member in Attribute class");
                    throw new ArgumentException(Environment.GetResourceString("Argument_InvalidParamInfo"));
            }
        }

Same methods

Attribute::IsDefined ( Assembly element, Type attributeType ) : bool
Attribute::IsDefined ( Assembly element, Type attributeType, bool inherit ) : bool
Attribute::IsDefined ( MemberInfo element, Type attributeType ) : bool
Attribute::IsDefined ( MemberInfo element, Type attributeType, bool inherit ) : bool
Attribute::IsDefined ( Module element, Type attributeType ) : bool
Attribute::IsDefined ( Module element, Type attributeType, bool inherit ) : bool
Attribute::IsDefined ( ParameterInfo element, Type attributeType ) : bool

Usage Example

        private static void ExtractOptions(EntityBase ent, IDictionary <string, List <DisplayOption> > dict)
        {
            var props = ent.GetType().GetPropertiesSorted();

            foreach (var info in props.Reverse())
            {
                //info
                if (!Attribute.IsDefined(info, typeof(EntityDescriptorAttribute)))
                {
                    continue;
                }
                var prop = (EntityDescriptorAttribute)info.GetCustomAttribute(typeof(EntityDescriptorAttribute));
                if (!dict.ContainsKey(prop.Category))
                {
                    dict.Add(prop.Category, new List <DisplayOption>());
                }

                object min = null, max = null;
                var    multiline = Attribute.IsDefined(info, typeof(MultilineStringAttribute));

                if (Attribute.IsDefined(info, typeof(MinMaxAttribute)))
                {
                    var att = (MinMaxAttribute)info.GetCustomAttribute(typeof(MinMaxAttribute));
                    min = att.Minimum;
                    max = att.Maximum;
                }

                dict[prop.Category].Insert(0,
                                           new DisplayOption(prop.Name, info.Name, info.PropertyType, ent, prop.Description, min, max, multiline, prop.IsEnabledPath, prop.FixedSize, prop.DataGridRowHeaderPath));
            }
        }