那么我们可以使用 TypeDescriptor string value = "123"; var typeDescriptor = TypeDescriptor.GetConverter (typeof(int)); int @int =(int) typeDescriptor.ConvertFromString(value); Console.WriteLine (typeDescriptor.ConvertToString(@int)); typeDescriptor = TypeDescriptor.GetConverter(typeof (double)); double @double = (double)typeDescriptor.ConvertFromString(value); Console.WriteLine(typeDescriptor.ConvertToString(@double)); 参见:http://www.jianshu.com/p/cdc8f5fe6405
去一个一个转换 实际上org.springframework.core.convert.ConversionService也为我们提供了参数为org.springframework.core.convert.TypeDescriptor import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor sourceType, TypeDescriptor targetType) { return CONVERSION_SERVICE.convert(source, sourceType (List.class, TypeDescriptor.valueOf(source.get(0).getClass())), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(targetClass))); } } 使用起来只需要: final List
那么我们可以使用 TypeDescriptor string value = "123"; var typeDescriptor = TypeDescriptor.GetConverter (typeof(int)); int @int =(int) typeDescriptor.ConvertFromString(value); Console.WriteLine (typeDescriptor.ConvertToString(@int)); typeDescriptor = TypeDescriptor.GetConverter(typeof (double)); double @double = (double)typeDescriptor.ConvertFromString(value); Console.WriteLine(typeDescriptor.ConvertToString(@double)); 参见:http://www.jianshu.com/p/cdc8f5fe6405
= null && typeDescriptor ! = null && typeDescriptor ! TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue); if (conversionService.canConvert typeDescriptor) { if (! elementType = (typeDescriptor !
public interface ConditionalConverter { boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType > source, TypeDescriptor streamType, TypeDescriptor targetType) { //这里是先把Stream转换为List List<Object sourceClass =TypeDescriptor.valueOf(User.class); TypeDescriptor targetClass = TypeDescriptor.valueOf source = TypeDescriptor.valueOf(String.class); TypeDescriptor target = TypeDescriptor.valueOf { public GenericTypeDescriptor(TypeDescriptor typeDescriptor) { super(typeDescriptor.getResolvableType
public TypeDescriptor getMapKeyTypeDescriptor(); public TypeDescriptor getMapValueTypeDescriptor() > mapType, @Nullable TypeDescriptor keyTypeDescriptor, @Nullable TypeDescriptor valueTypeDescriptor); public static TypeDescriptor array(@Nullable TypeDescriptor elementTypeDescriptor); public static TypeDescriptor nested(MethodParameter methodParameter, int nestingLevel); public static TypeDescriptor > targetType); boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType);
> targetType); boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType sourceType, TypeDescriptor targetType); } 其 UML 类图如下: ? getConverter() 如下: protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType); 到这里我们就可以得到 如下: public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType
(this, true); } public EventDescriptor GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent (this, true); } public string GetClassName() { return TypeDescriptor.GetClassName in them: public EventDescriptorCollection GetEvents(Attribute[] attributes) { return TypeDescriptor.GetEvents (this, true); } public TypeConverter GetConverter() { return TypeDescriptor.GetConverter (this, true); } public object GetEditor(Type editorBaseType) { return TypeDescriptor.GetEditor
typeDescriptor) throws TypeMismatchException { throw new UnsupportedOperationException("TypeDescriptor = null && typeDescriptor ! = null) { TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue); ClassUtils.isAssignableValue(requiredType, convertedValue))) { if (typeDescriptor ! > targetType); boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType)
> targetType); boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType); <T> T convert (Object source, Class<T> targetType); Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor TypeDescriptor.valueOf(sourceType) : null), TypeDescriptor.valueOf(targetType)); } @Override public boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { Assert.notNull Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { Assert.notNull(targetType
typeDescriptor) throws TypeMismatchException { throw new UnsupportedOperationException("TypeDescriptor = null && typeDescriptor ! = null) { TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue); if (conversionService.canConvert > targetType); boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType); sourceType, TypeDescriptor targetType); } UML类图 ?
TypeDescriptor.valueOf(sourceType) : null), TypeDescriptor.valueOf(targetType)); } @Override public boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { Assert.notNull convert(@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { sourceType, TypeDescriptor targetType) { // Check raw type first... sourceType; private final TypeDescriptor targetType; public ConverterCacheKey(TypeDescriptor sourceType
new TypeDescriptor(methodParam) : TypeDescriptor.valueOf(requiredType))); } @Override @Nullable new TypeDescriptor(field) : TypeDescriptor.valueOf(requiredType))); } //最终调用的方法 @Nullable @Override = null && typeDescriptor ! = null && typeDescriptor ! TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(mapKeyType); Object convertedMapKey = convertIfNecessary
) { if (typeDescriptor.startsWith("[")) { return toJavaTypeName(typeDescriptor.substring (1)) + "[]"; } else if (typeDescriptor.startsWith("L") && typeDescriptor.endsWith(";")) { return typeDescriptor.substring(1, typeDescriptor.length() - 1).replace('/', '.'); } else if ( primitiveTypes.containsKey(typeDescriptor)) { return primitiveTypes.get(typeDescriptor).getName (); } return typeDescriptor; } /** * 动态加载类 */ public static Class<?
sourceTypeDesp = TypeDescriptor.valueOf(Set.class); TypeDescriptor targetTypeDesp = TypeDescriptor.valueOf sourceType, TypeDescriptor targetType) { return (sourceType.getType() ! sourceType, TypeDescriptor targetType) { Method finder = getFinder(targetType.getType()); return ( = null && this.conversionService.canConvert(sourceType, TypeDescriptor.valueOf(finder.getParameterTypes sourceTypeDesp = TypeDescriptor.valueOf(String.class); TypeDescriptor targetTypeDesp = TypeDescriptor.valueOf
由此也能看出该转换器是可以支持N:N的(大多数情况下只写一对值而已,也有写多对的) TypeDescriptor:类型描述。 CollectionToCollectionConverter: @Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor sourceTypeDesp = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class)); TypeDescriptor targetTypeDesp = TypeDescriptor.collection(Set.class, TypeDescriptor.valueOf(Integer.class)); System.out.println public interface ConditionalConverter { boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType
由此也能看出该转换器是可以支持N:N的(大多数情况下只写一对值而已,也有写多对的) TypeDescriptor:类型描述。 CollectionToCollectionConverter: @Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor sourceTypeDesp = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class)); TypeDescriptor targetTypeDesp = TypeDescriptor.collection(Set.class, TypeDescriptor.valueOf(Integer.class)); System.out.println public interface ConditionalConverter { boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType
> targetType); boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType); <T> T convert 查找find @Nullable public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) { TypeDescriptor.valueOf(sourceType) : null), TypeDescriptor.valueOf(targetType)); } @Override public boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType ==
> targetType); boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType); <T> T convert 查找find @Nullable public GenericConverter find(TypeDescriptor sourceType, TypeDescriptor targetType) { TypeDescriptor.valueOf(sourceType) : null), TypeDescriptor.valueOf(targetType)); } @Override public boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType ==
sourceTypeDesp = TypeDescriptor.valueOf(Set.class); TypeDescriptor targetTypeDesp = TypeDescriptor.valueOf sourceType, TypeDescriptor targetType) { return (sourceType.getType() ! sourceType, TypeDescriptor targetType) { Method finder = getFinder(targetType.getType()); return ( = null && this.conversionService.canConvert(sourceType, TypeDescriptor.valueOf(finder.getParameterTypes sourceTypeDesp = TypeDescriptor.valueOf(String.class); TypeDescriptor targetTypeDesp = TypeDescriptor.valueOf