GetReflectionType() private method

private GetReflectionType ( Type type ) : Type
type System.Type
return System.Type
Exemplo n.º 1
0
 protected Attribute GetDefaultAttribute(Type attributeType)
 {
     lock (internalSyncObject)
     {
         if (_defaultAttributes == null)
         {
             _defaultAttributes = new Hashtable();
         }
         if (_defaultAttributes.ContainsKey(attributeType))
         {
             return((Attribute)_defaultAttributes[attributeType]);
         }
         Attribute attribute      = null;
         Type      reflectionType = TypeDescriptor.GetReflectionType(attributeType);
         FieldInfo field          = reflectionType.GetField("Default", BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static);
         if ((field != null) && field.IsStatic)
         {
             attribute = (Attribute)field.GetValue(null);
         }
         else
         {
             ConstructorInfo constructor = reflectionType.UnderlyingSystemType.GetConstructor(new Type[0]);
             if (constructor != null)
             {
                 attribute = (Attribute)constructor.Invoke(new object[0]);
                 if (!attribute.IsDefaultAttribute())
                 {
                     attribute = null;
                 }
             }
         }
         _defaultAttributes[attributeType] = attribute;
         return(attribute);
     }
 }
 public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
 {
     if (this.values == null)
     {
         Type reflectionType = TypeDescriptor.GetReflectionType(this.type);
         if (reflectionType == null)
         {
             reflectionType = this.type;
         }
         FieldInfo[] fields = reflectionType.GetFields(BindingFlags.Public | BindingFlags.Static);
         ArrayList   list   = null;
         if ((fields != null) && (fields.Length > 0))
         {
             list = new ArrayList(fields.Length);
         }
         if (list != null)
         {
             foreach (FieldInfo info in fields)
             {
                 BrowsableAttribute attribute = null;
                 foreach (Attribute attribute2 in info.GetCustomAttributes(typeof(BrowsableAttribute), false))
                 {
                     attribute = attribute2 as BrowsableAttribute;
                 }
                 if ((attribute == null) || attribute.Browsable)
                 {
                     object obj2 = null;
                     try
                     {
                         if (info.Name != null)
                         {
                             obj2 = Enum.Parse(this.type, info.Name);
                         }
                     }
                     catch (ArgumentException)
                     {
                     }
                     if (obj2 != null)
                     {
                         list.Add(obj2);
                     }
                 }
             }
             IComparer comparer = this.Comparer;
             if (comparer != null)
             {
                 list.Sort(comparer);
             }
         }
         Array values = (list != null) ? ((Array)list.ToArray()) : null;
         this.values = new TypeConverter.StandardValuesCollection(values);
     }
     return(this.values);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Returns the default value for an attribute. This uses the following heuristic:
        /// 1. It looks for a public static field named "Default".
        /// </summary>
        protected Attribute GetDefaultAttribute(Type attributeType)
        {
            if (attributeType == null)
            {
                throw new ArgumentNullException(nameof(attributeType));
            }

            lock (s_internalSyncObject)
            {
                if (s_defaultAttributes == null)
                {
                    s_defaultAttributes = new Hashtable();
                }

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

                Attribute attr = null;

                // Not in the table, so do the legwork to discover the default value.
                Type      reflect = TypeDescriptor.GetReflectionType(attributeType);
                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(Array.Empty <Type>());
                    if (ci != null)
                    {
                        attr = (Attribute)ci.Invoke(Array.Empty <object>());

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

                s_defaultAttributes[attributeType] = attr;
                return(attr);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns the default value for an attribute. This uses the following heuristic:
        /// 1. It looks for a public static field named "Default".
        /// </summary>
        protected Attribute?GetDefaultAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicFields)] Type attributeType)
        {
            ArgumentNullException.ThrowIfNull(attributeType);

            lock (s_internalSyncObject)
            {
                if (s_defaultAttributes == null)
                {
                    s_defaultAttributes = new Dictionary <Type, Attribute?>();
                }

                // If we have already encountered this, use what's in the table.
                if (s_defaultAttributes.TryGetValue(attributeType, out Attribute? defaultAttribute))
                {
                    return(defaultAttribute);
                }

                Attribute?attr = null;

                // Not in the table, so do the legwork to discover the default value.
                Type      reflect = TypeDescriptor.GetReflectionType(attributeType);
                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(Type.EmptyTypes);
                    if (ci != null)
                    {
                        attr = (Attribute)ci.Invoke(Array.Empty <object>());

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

                s_defaultAttributes[attributeType] = attr;
                return(attr);
            }
        }
Exemplo n.º 5
0
        /// <internalonly/>
        /// <devdoc>
        ///    <para>Gets a collection of standard values for the data type this validator is
        ///       designed for.</para>
        /// </devdoc>
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            if (values == null)
            {
                // We need to get the enum values in this rather round-about way so we can filter
                // out fields marked Browsable(false). Note that if multiple fields have the same value,
                // the behavior is undefined, since what we return are just enum values, not names.

                Type reflectType = TypeDescriptor.GetReflectionType(type);
                if (reflectType == null)
                {
                    reflectType = type;
                }

                FieldInfo[] fields    = reflectType.GetFields(BindingFlags.Public | BindingFlags.Static);
                ArrayList   objValues = null;

                if (fields != null && fields.Length > 0)
                {
                    objValues = new ArrayList(fields.Length);
                }

                if (objValues != null)
                {
                    foreach (FieldInfo field in fields)
                    {
                        BrowsableAttribute browsableAttr = null;
                        foreach (Attribute attr in field.GetCustomAttributes(typeof(BrowsableAttribute), false))
                        {
                            browsableAttr = attr as BrowsableAttribute;
                        }

                        if (browsableAttr == null || browsableAttr.Browsable)
                        {
                            object value = null;

                            try {
                                if (field.Name != null)
                                {
                                    value = Enum.Parse(type, field.Name);
                                }
                            }
                            catch (ArgumentException) {
                                // Hmm, for some reason, the parse threw. Let us ignore this value.
                            }

                            if (value != null)
                            {
                                objValues.Add(value);
                            }
                        }
                    }

                    IComparer comparer = Comparer;
                    if (comparer != null)
                    {
                        objValues.Sort(comparer);
                    }
                }

                Array arr = (objValues != null) ? objValues.ToArray() : null;
                values = new StandardValuesCollection(arr);
            }
            return(values);
        }