private static PropertyDescriptor[] ReflectGetExtendedProperties(IExtenderProvider provider) { PropertyDescriptor[] descriptorArray; IDictionary cache = TypeDescriptor.GetCache(provider); if (cache != null) { descriptorArray = cache[_extenderProviderPropertiesKey] as PropertyDescriptor[]; if (descriptorArray != null) { return descriptorArray; } } if (_extendedPropertyCache == null) { lock (_internalSyncObject) { if (_extendedPropertyCache == null) { _extendedPropertyCache = new Hashtable(); } } } Type componentType = provider.GetType(); ReflectPropertyDescriptor[] array = (ReflectPropertyDescriptor[]) _extendedPropertyCache[componentType]; if (array == null) { lock (_internalSyncObject) { array = (ReflectPropertyDescriptor[]) _extendedPropertyCache[componentType]; if (array == null) { AttributeCollection attributes = TypeDescriptor.GetAttributes(componentType); ArrayList list = new ArrayList(attributes.Count); foreach (Attribute attribute in attributes) { ProvidePropertyAttribute attribute2 = attribute as ProvidePropertyAttribute; if (attribute2 != null) { Type typeFromName = GetTypeFromName(attribute2.ReceiverTypeName); if (typeFromName != null) { MethodInfo method = componentType.GetMethod("Get" + attribute2.PropertyName, new Type[] { typeFromName }); if (((method != null) && !method.IsStatic) && method.IsPublic) { MethodInfo setMethod = componentType.GetMethod("Set" + attribute2.PropertyName, new Type[] { typeFromName, method.ReturnType }); if ((setMethod != null) && (setMethod.IsStatic || !setMethod.IsPublic)) { setMethod = null; } list.Add(new ReflectPropertyDescriptor(componentType, attribute2.PropertyName, method.ReturnType, typeFromName, method, setMethod, null)); } } } } array = new ReflectPropertyDescriptor[list.Count]; list.CopyTo(array, 0); _extendedPropertyCache[componentType] = array; } } } descriptorArray = new PropertyDescriptor[array.Length]; for (int i = 0; i < array.Length; i++) { Attribute[] attributeArray = null; IComponent component = provider as IComponent; if ((component == null) || (component.Site == null)) { attributeArray = new Attribute[] { DesignOnlyAttribute.Yes }; } ReflectPropertyDescriptor extenderInfo = array[i]; descriptorArray[i] = new ExtendedPropertyDescriptor(extenderInfo, extenderInfo.ExtenderGetReceiverType(), provider, attributeArray); } if (cache != null) { cache[_extenderProviderPropertiesKey] = descriptorArray; } return descriptorArray; }
public ExtendedPropertyDescriptor(PropertyDescriptor extender, Attribute[] attributes) : base(extender, attributes) { ExtenderProvidedPropertyAttribute attribute = extender.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute; ReflectPropertyDescriptor extenderProperty = attribute.ExtenderProperty as ReflectPropertyDescriptor; this.extenderInfo = extenderProperty; this.provider = attribute.Provider; }
public ExtendedPropertyDescriptor(PropertyDescriptor extender, Attribute[] attributes) : base(extender, attributes) { ExtenderProvidedPropertyAttribute attribute = extender.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute; ReflectPropertyDescriptor extenderProperty = attribute.ExtenderProperty as ReflectPropertyDescriptor; this.extenderInfo = extenderProperty; this.provider = attribute.Provider; }
/// <include file='doc\ReflectPropertyDescriptor.uex' path='docs/doc[@for="ReflectPropertyDescriptor.ReflectPropertyDescriptor4"]/*' /> /// <devdoc> /// This constructor takes an existing ReflectPropertyDescriptor and modifies it by merging in the /// passed-in attributes. /// </devdoc> public ReflectPropertyDescriptor(Type componentClass, PropertyDescriptor oldReflectPropertyDescriptor, Attribute[] attributes) : base(oldReflectPropertyDescriptor, attributes) { this.componentClass = componentClass; this.type = oldReflectPropertyDescriptor.PropertyType; if (componentClass == null) { throw new ArgumentException(SR.GetString(SR.InvalidNullArgument, "componentClass")); } // If the classes are the same, we can potentially optimize the method fetch because // the old property descriptor may already have it. // if (oldReflectPropertyDescriptor is ReflectPropertyDescriptor) { ReflectPropertyDescriptor oldProp = (ReflectPropertyDescriptor)oldReflectPropertyDescriptor; if (oldProp.ComponentType == componentClass) { propInfo = oldProp.propInfo; getMethod = oldProp.getMethod; setMethod = oldProp.setMethod; shouldSerializeMethod = oldProp.shouldSerializeMethod; resetMethod = oldProp.resetMethod; defaultValue = oldProp.defaultValue; ambientValue = oldProp.ambientValue; state = oldProp.state; } // Now we must figure out what to do with our default value. First, check to see // if the caller has provided an new default value attribute. If so, use it. Otherwise, // just let it be and it will be picked up on demand. // if (attributes != null) { foreach (Attribute a in attributes) { if (a is DefaultValueAttribute) { defaultValue = ((DefaultValueAttribute)a).Value; state[BitDefaultValueQueried] = true; } else if (a is AmbientValueAttribute) { ambientValue = ((AmbientValueAttribute)a).Value; state[BitAmbientValueQueried] = true; } } } } }
public ExtendedPropertyDescriptor(ReflectPropertyDescriptor extenderInfo, Type receiverType, IExtenderProvider provider, Attribute[] attributes) : base(extenderInfo, attributes) { ArrayList list = new ArrayList(this.AttributeArray); list.Add(ExtenderProvidedPropertyAttribute.Create(extenderInfo, receiverType, provider)); if (extenderInfo.IsReadOnly) { list.Add(ReadOnlyAttribute.Yes); } Attribute[] array = new Attribute[list.Count]; list.CopyTo(array, 0); this.AttributeArray = array; this.extenderInfo = extenderInfo; this.provider = provider; }
public ExtendedPropertyDescriptor(PropertyDescriptor extender, Attribute[] attributes) : base(extender, attributes) { Debug.Assert(extender != null, "The original PropertyDescriptor must be non-null"); ExtenderProvidedPropertyAttribute attr = extender.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute; Debug.Assert(attr != null, "The original PropertyDescriptor does not have an ExtenderProvidedPropertyAttribute"); ReflectPropertyDescriptor reflectDesc = attr.ExtenderProperty as ReflectPropertyDescriptor; Debug.Assert(reflectDesc != null, "The original PropertyDescriptor has an invalid ExtenderProperty"); this.extenderInfo = reflectDesc; this.provider = attr.Provider; }
public ExtendedPropertyDescriptor(ReflectPropertyDescriptor extenderInfo, Type receiverType, IExtenderProvider provider, Attribute[] attributes) : base(extenderInfo, attributes) { ArrayList list = new ArrayList(this.AttributeArray); list.Add(ExtenderProvidedPropertyAttribute.Create(extenderInfo, receiverType, provider)); if (extenderInfo.IsReadOnly) { list.Add(ReadOnlyAttribute.Yes); } Attribute[] array = new Attribute[list.Count]; list.CopyTo(array, 0); this.AttributeArray = array; this.extenderInfo = extenderInfo; this.provider = provider; }
public ExtendedPropertyDescriptor(PropertyDescriptor extender, Attribute[] attributes) : base(extender, attributes) { Debug.Assert(extender != null, "The original PropertyDescriptor must be non-null"); ExtenderProvidedPropertyAttribute attr = extender.Attributes[typeof(ExtenderProvidedPropertyAttribute)] as ExtenderProvidedPropertyAttribute; Debug.Assert(attr != null, "The original PropertyDescriptor does not have an ExtenderProvidedPropertyAttribute"); ReflectPropertyDescriptor reflectDesc = attr.ExtenderProperty as ReflectPropertyDescriptor; Debug.Assert(reflectDesc != null, "The original PropertyDescriptor has an invalid ExtenderProperty"); _extenderInfo = reflectDesc; _provider = attr.Provider; }
public ReflectPropertyDescriptor(Type componentClass, PropertyDescriptor oldReflectPropertyDescriptor, Attribute[] attributes) : base(oldReflectPropertyDescriptor, attributes) { this.state = new BitVector32(); this.componentClass = componentClass; this.type = oldReflectPropertyDescriptor.PropertyType; if (componentClass == null) { throw new ArgumentException(SR.GetString("InvalidNullArgument", new object[] { "componentClass" })); } ReflectPropertyDescriptor descriptor = oldReflectPropertyDescriptor as ReflectPropertyDescriptor; if (descriptor != null) { if (descriptor.ComponentType == componentClass) { this.propInfo = descriptor.propInfo; this.getMethod = descriptor.getMethod; this.setMethod = descriptor.setMethod; this.shouldSerializeMethod = descriptor.shouldSerializeMethod; this.resetMethod = descriptor.resetMethod; this.defaultValue = descriptor.defaultValue; this.ambientValue = descriptor.ambientValue; this.state = descriptor.state; } if (attributes != null) { foreach (Attribute attribute in attributes) { DefaultValueAttribute attribute2 = attribute as DefaultValueAttribute; if (attribute2 != null) { this.defaultValue = attribute2.Value; this.state[BitDefaultValueQueried] = true; } else { AmbientValueAttribute attribute3 = attribute as AmbientValueAttribute; if (attribute3 != null) { this.ambientValue = attribute3.Value; this.state[BitAmbientValueQueried] = true; } } } } } }
private readonly IExtenderProvider provider; // the guy providing it /// <devdoc> /// Creates a new extended property info. Callers can then treat this as /// a standard property. /// </devdoc> public ExtendedPropertyDescriptor(ReflectPropertyDescriptor extenderInfo, Type receiverType, IExtenderProvider provider, Attribute[] attributes) : base(extenderInfo, attributes) { Debug.Assert(extenderInfo != null, "ExtendedPropertyDescriptor must have extenderInfo"); Debug.Assert(provider != null, "ExtendedPropertyDescriptor must have provider"); ArrayList attrList = new ArrayList(AttributeArray); attrList.Add(ExtenderProvidedPropertyAttribute.Create(extenderInfo, receiverType, provider)); if (extenderInfo.IsReadOnly) { attrList.Add(ReadOnlyAttribute.Yes); } Attribute[] temp = new Attribute[attrList.Count]; attrList.CopyTo(temp, 0); AttributeArray = temp; this.extenderInfo = extenderInfo; this.provider = provider; }
private readonly IExtenderProvider _provider; // the guy providing it /// <summary> /// Creates a new extended property info. Callers can then treat this as /// a standard property. /// </summary> public ExtendedPropertyDescriptor(ReflectPropertyDescriptor extenderInfo, Type receiverType, IExtenderProvider provider, Attribute[] attributes) : base(extenderInfo, attributes) { Debug.Assert(extenderInfo != null, "ExtendedPropertyDescriptor must have extenderInfo"); Debug.Assert(provider != null, "ExtendedPropertyDescriptor must have provider"); ArrayList attrList = new ArrayList(AttributeArray); attrList.Add(ExtenderProvidedPropertyAttribute.Create(extenderInfo, receiverType, provider)); if (extenderInfo.IsReadOnly) { attrList.Add(ReadOnlyAttribute.Yes); } Attribute[] temp = new Attribute[attrList.Count]; attrList.CopyTo(temp, 0); AttributeArray = temp; _extenderInfo = extenderInfo; _provider = provider; }
/// <devdoc> /// Static helper API around reflection to get and cache /// properties. This does not recurse to the base class. /// </devdoc> private static PropertyDescriptor[] ReflectGetProperties(Type type) { if (_propertyCache == null) { lock(_internalSyncObject) { if (_propertyCache == null) { _propertyCache = new Hashtable(); } } } PropertyDescriptor[] properties = (PropertyDescriptor[])_propertyCache[type]; if (properties != null) { return properties; } lock (_internalSyncObject) { properties = (PropertyDescriptor[])_propertyCache[type]; if (properties == null) { BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance; TypeDescriptor.Trace("Properties : Building properties for {0}", type.Name); // Get the type's properties. Properties may have their // get and set methods individually overridden in a derived // class, so if we find a missing method we need to walk // down the base class chain to find it. We actually merge // "new" properties of the same name, so we must preserve // the member info for each method individually. // PropertyInfo[] propertyInfos = type.GetProperties(bindingFlags); properties = new PropertyDescriptor[propertyInfos.Length]; int propertyCount = 0; for (int idx = 0; idx < propertyInfos.Length; idx++) { PropertyInfo propertyInfo = propertyInfos[idx]; // Today we do not support parameterized properties. // if (propertyInfo.GetIndexParameters().Length > 0) { continue; } MethodInfo getMethod = propertyInfo.GetGetMethod(); MethodInfo setMethod = propertyInfo.GetSetMethod(); string name = propertyInfo.Name; // If the property only overrode "set", then we don't // pick it up here. Rather, we just merge it in from // the base class list. // If a property had at least a get method, we consider it. We don't // consider write-only properties. // if (getMethod != null) { properties[propertyCount++] = new ReflectPropertyDescriptor(type, name, propertyInfo.PropertyType, propertyInfo, getMethod, setMethod, null); } } if (propertyCount != properties.Length) { PropertyDescriptor[] newProperties = new PropertyDescriptor[propertyCount]; Array.Copy(properties, 0, newProperties, 0, propertyCount); properties = newProperties; } #if DEBUG foreach(PropertyDescriptor dbgProp in properties) { Debug.Assert(dbgProp != null, "Holes in property array for type " + type); } #endif _propertyCache[type] = properties; } } return properties; }
/// <devdoc> /// This performs the actual reflection needed to discover /// extender properties. If object caching is supported this /// will maintain a cache of property descriptors on the /// extender provider. Extender properties are actually two /// property descriptors in one. There is a chunk of per-class /// data in a ReflectPropertyDescriptor that defines the /// parameter types and get and set methods of the extended property, /// and there is an ExtendedPropertyDescriptor that combines this /// with an extender provider object to create what looks like a /// normal property. ReflectGetExtendedProperties maintains two /// separate caches for these two sets: a static one for the /// ReflectPropertyDescriptor values that don't change for each /// provider instance, and a per-provider cache that contains /// the ExtendedPropertyDescriptors. /// </devdoc> private static PropertyDescriptor[] ReflectGetExtendedProperties(IExtenderProvider provider) { IDictionary cache = TypeDescriptor.GetCache(provider); PropertyDescriptor[] properties; if (cache != null) { properties = cache[_extenderProviderPropertiesKey] as PropertyDescriptor[]; if (properties != null) { return properties; } } // Our per-instance cache missed. We have never seen this instance of the // extender provider before. See if we can find our class-based // property store. // if (_extendedPropertyCache == null) { lock (_internalSyncObject) { if (_extendedPropertyCache == null) { _extendedPropertyCache = new Hashtable(); } } } Type providerType = provider.GetType(); ReflectPropertyDescriptor[] extendedProperties = (ReflectPropertyDescriptor[])_extendedPropertyCache[providerType]; if (extendedProperties == null) { lock (_internalSyncObject) { extendedProperties = (ReflectPropertyDescriptor[])_extendedPropertyCache[providerType]; // Our class-based property store failed as well, so we need to build up the set of // extended properties here. // if (extendedProperties == null) { AttributeCollection attributes = TypeDescriptor.GetAttributes(providerType); ArrayList extendedList = new ArrayList(attributes.Count); foreach(Attribute attr in attributes) { ProvidePropertyAttribute provideAttr = attr as ProvidePropertyAttribute; if (provideAttr != null) { Type receiverType = GetTypeFromName(provideAttr.ReceiverTypeName); if (receiverType != null) { MethodInfo getMethod = providerType.GetMethod("Get" + provideAttr.PropertyName, new Type[] {receiverType}); if (getMethod != null && !getMethod.IsStatic && getMethod.IsPublic) { MethodInfo setMethod = providerType.GetMethod("Set" + provideAttr.PropertyName, new Type[] {receiverType, getMethod.ReturnType}); if (setMethod != null && (setMethod.IsStatic || !setMethod.IsPublic)) { setMethod = null; } extendedList.Add(new ReflectPropertyDescriptor(providerType, provideAttr.PropertyName, getMethod.ReturnType, receiverType, getMethod, setMethod, null)); } } } } extendedProperties = new ReflectPropertyDescriptor[extendedList.Count]; extendedList.CopyTo(extendedProperties, 0); _extendedPropertyCache[providerType] = extendedProperties; } } } // Now that we have our extended properties we can build up a list of callable properties. These can be // returned to the user. // properties = new PropertyDescriptor[extendedProperties.Length]; for (int idx = 0; idx < extendedProperties.Length; idx++) { Attribute[] attrs = null; IComponent comp = provider as IComponent; if (comp == null || comp.Site == null) { attrs = new Attribute[] {DesignOnlyAttribute.Yes}; } ReflectPropertyDescriptor rpd = extendedProperties[idx]; ExtendedPropertyDescriptor epd = new ExtendedPropertyDescriptor(rpd, rpd.ExtenderGetReceiverType(), provider, attrs); properties[idx] = epd; } if (cache != null) { cache[_extenderProviderPropertiesKey] = properties; } return properties; }
private readonly IExtenderProvider provider; // the guy providing it /// <include file='doc\ExtendedPropertyDescriptor.uex' path='docs/doc[@for="ExtendedPropertyDescriptor.ExtendedPropertyDescriptor"]/*' /> /// <devdoc> /// Creates a new extended property info. Callers can then treat this as /// a standard property. /// </devdoc> public ExtendedPropertyDescriptor(ReflectPropertyDescriptor extenderInfo, Type receiverType, IExtenderProvider provider) : this(extenderInfo, receiverType, provider, null) { }
private static PropertyDescriptor[] ReflectGetProperties(Type type) { if (_propertyCache == null) { lock (_internalSyncObject) { if (_propertyCache == null) { _propertyCache = new Hashtable(); } } } PropertyDescriptor[] sourceArray = (PropertyDescriptor[]) _propertyCache[type]; if (sourceArray == null) { lock (_internalSyncObject) { sourceArray = (PropertyDescriptor[]) _propertyCache[type]; if (sourceArray != null) { return sourceArray; } BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly; PropertyInfo[] properties = type.GetProperties(bindingAttr); sourceArray = new PropertyDescriptor[properties.Length]; int length = 0; for (int i = 0; i < properties.Length; i++) { PropertyInfo propInfo = properties[i]; if (propInfo.GetIndexParameters().Length <= 0) { MethodInfo getMethod = propInfo.GetGetMethod(); MethodInfo setMethod = propInfo.GetSetMethod(); string name = propInfo.Name; if (getMethod != null) { sourceArray[length++] = new ReflectPropertyDescriptor(type, name, propInfo.PropertyType, propInfo, getMethod, setMethod, null); } } } if (length != sourceArray.Length) { PropertyDescriptor[] destinationArray = new PropertyDescriptor[length]; Array.Copy(sourceArray, 0, destinationArray, 0, length); sourceArray = destinationArray; } _propertyCache[type] = sourceArray; } } return sourceArray; }