在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
public static object ChangeType(object value, Type targetType) { if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof (Nullable<>)) { NullableConverter nullableConverter = new NullableConverter(targetType); Type convertType = nullableConverter.UnderlyingType; return Convert.ChangeType(value, convertType); } if (value == null && targetType.IsGenericType) { return Activator.CreateInstance(targetType); } if (value == null) { return null; } if (targetType == value.GetType()) { return value; } if (targetType.IsEnum) { if (value is string) { return Enum.Parse(targetType, value as string); } else { return Enum.ToObject(targetType, value); } } if (!targetType.IsInterface && targetType.IsGenericType) { Type innerType = targetType.GetGenericArguments()[0]; object innerValue = ChangeType(value, innerType); return Activator.CreateInstance(targetType, new object[] { innerValue }); } if (value is string && targetType == typeof (Guid)) { return new Guid(value as string); } if (value is string && targetType == typeof (Version)) { return new Version(value as string); } if (!(value is IConvertible)) { return value; } return Convert.ChangeType(value, targetType); }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论