private object ConvertViaSerialization(object source, Type sourceType, Type targetType) { object obj2; if ((!sourceType.IsSerializable || !sourceType.Assembly.FullName.StartsWith("query_")) || (UserCache.InferKeyFromType(sourceType) != UserCache.InferKeyFromType(targetType))) { throw new UserCache.TypeChangeException(); } byte[] bytes = Encoding.ASCII.GetBytes(sourceType.Assembly.FullName); byte[] buffer2 = Encoding.ASCII.GetBytes(targetType.Assembly.FullName); if (bytes.Length != buffer2.Length) { throw new UserCache.TypeChangeException(); } IFormatter formatter = new BinaryFormatter(); MemoryStream serializationStream = new MemoryStream(); formatter.Serialize(serializationStream, source); byte[] buffer = serializationStream.ToArray(); for (int i = 0; i < (buffer.Length - bytes.Length); i++) { bool flag = false; int index = 0; while (index < bytes.Length) { if (buffer[i + index] != bytes[index]) { goto Label_00C4; } index++; } goto Label_00C7; Label_00C4: flag = true; Label_00C7: if (!flag) { for (index = 0; index < bytes.Length; index++) { buffer[i + index] = buffer2[index]; } } } serializationStream = new MemoryStream(buffer); try { obj2 = formatter.Deserialize(serializationStream); } catch (ArgumentException) { throw new UserCache.TypeChangeException(); } return(obj2); }
private bool CanConvert(Type sourceType, Type targetType, HashSet <TypeType> visitedTypes) { if ((targetType != sourceType) && !targetType.IsAssignableFrom(sourceType)) { TypeType item = new TypeType { SourceType = sourceType, TargetType = targetType }; if (visitedTypes.Contains(item)) { return(true); } visitedTypes.Add(item); if (!targetType.IsAnonymous()) { if (targetType.IsArray) { if (!sourceType.IsArray) { return(false); } return(this.CanConvert(sourceType.GetElementType(), targetType.GetElementType(), visitedTypes)); } Type type3 = null; if (targetType.IsGenericType) { type3 = this.GetGenericTypeDefinition(targetType); } if (((type3 == typeof(IEnumerable <>)) || (type3 == typeof(IList <>))) || (type3 == typeof(List <>))) { Type arg = this.GetGenericIEnumerableFromType(sourceType); if (arg == null) { return(false); } Type type5 = this.GetFirstGenericArg(arg); Type type6 = this.GetFirstGenericArg(targetType); return(this.CanConvert(type5, type6, visitedTypes)); } if (!targetType.IsSerializable || !targetType.Assembly.FullName.StartsWith("query_")) { throw this.GetUncacheableException(targetType); } if (!(sourceType.IsSerializable && sourceType.Assembly.FullName.StartsWith("query_"))) { return(false); } if (UserCache.InferKeyFromType(sourceType) != UserCache.InferKeyFromType(targetType)) { return(false); } IEnumerable <string> enumerable = from m in sourceType.GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) select m.ToString(); IEnumerable <string> enumerable2 = from m in targetType.GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) select m.ToString(); return((from m in enumerable orderby m select m).SequenceEqual <string>((from m in enumerable2 orderby m select m))); } if (!sourceType.IsAnonymous()) { return(false); } PropertyInfo[] infoArray = (from p in sourceType.GetProperties() orderby p.Name select p).ToArray <PropertyInfo>(); PropertyInfo[] infoArray2 = (from p in targetType.GetProperties() orderby p.Name select p).ToArray <PropertyInfo>(); if (!(from p in infoArray select p.Name).SequenceEqual <string>((from p in infoArray2 select p.Name))) { return(false); } for (int i = 0; i < infoArray.Length; i++) { if (!this.CanConvert(infoArray[i].PropertyType, infoArray2[i].PropertyType, visitedTypes)) { return(false); } } } return(true); }