/// <summary> /// Get the value serializer declared for the given property. ValueSerializer can be overriden by an attribute /// on the property declaration. /// </summary> /// <param name="descriptor">PropertyDescriptor for the property to be serialized</param> /// <returns>A value serializer associated with the given property</returns> public static ValueSerializer GetSerializerFor(PropertyDescriptor descriptor) { ValueSerializer result; if (descriptor == null) { throw new ArgumentNullException(nameof(descriptor)); } #pragma warning suppress 6506 // descriptor is obviously not null ValueSerializerAttribute serializerAttribute = descriptor.Attributes[typeof(ValueSerializerAttribute)] as ValueSerializerAttribute; if (serializerAttribute != null) { result = (ValueSerializer)Activator.CreateInstance(serializerAttribute.ValueSerializerType); } else { result = GetSerializerFor(descriptor.PropertyType); if (result == null || result is TypeConverterValueSerializer) { TypeConverter converter = descriptor.Converter; if (converter != null && converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string)) && !(converter is ReferenceConverter)) { result = new TypeConverterValueSerializer(converter); } } } return(result); }
/// <summary> /// Get the value serializer declared for the given type. /// </summary> /// <param name="type">The value type to serialize</param> /// <returns>The value serializer associated with the given type</returns> public static ValueSerializer GetSerializerFor(Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } object value = _valueSerializers[type]; if (value != null) { // This uses _valueSerializersLock's instance as a sentinal for null (as opposed to not attempted yet). return(value == _valueSerializersLock ? null : value as ValueSerializer); } AttributeCollection attributes = TypeDescriptor.GetAttributes(type); ValueSerializerAttribute attribute = attributes[typeof(ValueSerializerAttribute)] as ValueSerializerAttribute; ValueSerializer result = null; if (attribute != null) { result = (ValueSerializer)Activator.CreateInstance(attribute.ValueSerializerType); } if (result == null) { if (type == typeof(string)) { result = new StringValueSerializer(); } else { // Try to use the type converter TypeConverter converter = TypeConverterHelper.GetTypeConverter(type); // DateTime is a special-case. We can't use the DateTimeConverter, because it doesn't // support anything other than user culture and invariant culture, and we need to specify // en-us culture. if (converter.GetType() == typeof(DateTimeConverter2)) { result = new DateTimeValueSerializer(); } else if (converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string)) && !(converter is ReferenceConverter)) { result = new TypeConverterValueSerializer(converter); } } } lock (_valueSerializersLock) { // This uses _valueSerializersLock's instance as a sentinal for null (as opposed to not attempted yet). _valueSerializers[type] = result == null ? _valueSerializersLock : result; } return(result); }
public static ValueSerializer GetSerializerFor(Type type) { if (type == null) { throw new ArgumentNullException("type"); } object obj2 = _valueSerializers[type]; if (obj2 != null) { if (obj2 != _valueSerializersLock) { return (obj2 as ValueSerializer); } return null; } ValueSerializerAttribute attribute = TypeDescriptor.GetAttributes(type)[typeof(ValueSerializerAttribute)] as ValueSerializerAttribute; ValueSerializer serializer = null; if (attribute != null) { serializer = (ValueSerializer) Activator.CreateInstance(attribute.ValueSerializerType); } if (serializer == null) { if (type == typeof(string)) { serializer = new StringValueSerializer(); } else { TypeConverter typeConverter = TypeConverterHelper.GetTypeConverter(type); if (typeConverter.GetType() == typeof(DateTimeConverter2)) { serializer = new DateTimeValueSerializer(); } else if ((typeConverter.CanConvertTo(typeof(string)) && typeConverter.CanConvertFrom(typeof(string))) && !(typeConverter is ReferenceConverter)) { serializer = new TypeConverterValueSerializer(typeConverter); } } } lock (_valueSerializersLock) { _valueSerializers[type] = (serializer == null) ? _valueSerializersLock : serializer; } return serializer; }
public static ValueSerializer GetSerializerFor(PropertyDescriptor descriptor) { if (descriptor == null) { throw new ArgumentNullException("descriptor"); } ValueSerializerAttribute attribute = descriptor.Attributes[typeof(ValueSerializerAttribute)] as ValueSerializerAttribute; if (attribute != null) { return (ValueSerializer) Activator.CreateInstance(attribute.ValueSerializerType); } ValueSerializer serializerFor = GetSerializerFor(descriptor.PropertyType); if ((serializerFor == null) || (serializerFor is TypeConverterValueSerializer)) { TypeConverter converter = descriptor.Converter; if (((converter != null) && converter.CanConvertTo(typeof(string))) && (converter.CanConvertFrom(typeof(string)) && !(converter is ReferenceConverter))) { serializerFor = new TypeConverterValueSerializer(converter); } } return serializerFor; }