System.ComponentModel.AttributeCollection.GetDefaultAttribute C# (CSharp) Method

GetDefaultAttribute() protected method

protected GetDefaultAttribute ( Type attributeType ) : Attribute
attributeType System.Type
return System.Attribute
        protected Attribute GetDefaultAttribute(Type attributeType)
        {
            lock (internalSyncObject)
            {
                if (_defaultAttributes == null)
                {
                    _defaultAttributes = new Hashtable();
                }

                // If we have already encountered this, use what's in the
                // table.
                if (_defaultAttributes.ContainsKey(attributeType))
                {
                    return(Attribute)_defaultAttributes[attributeType];
                }

                Attribute attr = null;

                // Nope, not in the table, so do the legwork to discover the default value.
                Type reflect = TypeDescriptor.GetReflectionType(attributeType);
                System.Reflection.FieldInfo field = reflect.GetField("Default", BindingFlags.Public | BindingFlags.Static | BindingFlags.GetField);
                if (field != null && field.IsStatic)
                {
                    attr = (Attribute)field.GetValue(null);
                }
                else
                {
                    ConstructorInfo ci = reflect.UnderlyingSystemType.GetConstructor(new Type[0]);
                    if (ci != null)
                    {
                        attr = (Attribute)ci.Invoke(new object[0]);

                        // If we successfully created, verify that it is the
                        // default.  Attributes don't have to abide by this rule.
                        if (!attr.IsDefaultAttribute())
                        {
                            attr = null;
                        }
                    }
                }

                _defaultAttributes[attributeType] = attr;
                return attr;
            }
        }