public ReflectPropertyDescriptor(Type componentClass, PropertyDescriptor oldReflectPropertyDescriptor, Attribute[] attributes)
: base(oldReflectPropertyDescriptor, attributes)
{
_componentClass = componentClass;
_type = oldReflectPropertyDescriptor.PropertyType;
if (componentClass == null)
{
throw new ArgumentException(string.Format(SR.InvalidNullArgument, nameof(componentClass)));
}
// If the classes are the same, we can potentially optimize the method fetch because
// the old property descriptor may already have it.
//
ReflectPropertyDescriptor oldProp = oldReflectPropertyDescriptor as ReflectPropertyDescriptor;
if (oldProp != null)
{
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)
{
DefaultValueAttribute dva = a as DefaultValueAttribute;
if (dva != null)
{
_defaultValue = dva.Value;
// Default values for enums are often stored as their underlying integer type:
if (_defaultValue != null && PropertyType.GetTypeInfo().IsEnum && PropertyType.GetTypeInfo().GetEnumUnderlyingType() == _defaultValue.GetType())
{
_defaultValue = Enum.ToObject(PropertyType, _defaultValue);
}
_state.DangerousSet(s_bitDefaultValueQueried, true);
}
else
{
AmbientValueAttribute ava = a as AmbientValueAttribute;
if (ava != null)
{
_ambientValue = ava.Value;
_state.DangerousSet(s_bitAmbientValueQueried, true);
}
}
}
}
}
}