/// <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); }
/// <summary>Gets the <see cref="T:System.Windows.Markup.ValueSerializer" /> declared for the specified type.</summary> /// <returns>The serializer associated with the specified type. May return null.</returns> /// <param name="type">The type to get the <see cref="T:System.Windows.Markup.ValueSerializer" /> for.</param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="type" /> is null.</exception> public static ValueSerializer GetSerializerFor(Type type) { if (type == null) { throw new ArgumentNullException("type"); } object obj = _valueSerializers[type]; if (obj != null) { if (obj != _valueSerializersLock) { return(obj as ValueSerializer); } return(null); } AttributeCollection attributes = TypeDescriptor.GetAttributes(type); ValueSerializerAttribute valueSerializerAttribute = attributes[typeof(ValueSerializerAttribute)] as ValueSerializerAttribute; ValueSerializer valueSerializer = null; if (valueSerializerAttribute != null) { valueSerializer = (ValueSerializer)Activator.CreateInstance(valueSerializerAttribute.ValueSerializerType); } if (valueSerializer == null) { if (type == typeof(string)) { valueSerializer = new StringValueSerializer(); } else { TypeConverter typeConverter = TypeConverterHelper.GetTypeConverter(type); if (typeConverter.GetType() == typeof(DateTimeConverter2)) { valueSerializer = new DateTimeValueSerializer(); } else if (typeConverter.CanConvertTo(typeof(string)) && typeConverter.CanConvertFrom(typeof(string)) && !(typeConverter is ReferenceConverter)) { valueSerializer = new TypeConverterValueSerializer(typeConverter); } } } lock (_valueSerializersLock) { _valueSerializers[type] = ((valueSerializer == null) ? _valueSerializersLock : valueSerializer); return(valueSerializer); } }
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; }