private static bool TryParseSubRange(IDataType baseType, TypeAttributeCollection attributes, IBinder binder, out ISubRangeType subRange) { string str; string str2; if (((attributes != null) && ((attributes.Count >= 2) && (baseType.Category == DataTypeCategory.Primitive))) && (attributes.TryGetValue("LowerBorder", out str2) & attributes.TryGetValue("UpperBorder", out str))) { object obj2; object obj3; IManagedMappableType type = (IManagedMappableType)baseType; System.Type managedType = type.ManagedType; System.Type type3 = type.ManagedType; if (managedType == typeof(byte)) { type3 = typeof(sbyte); } else if (managedType == typeof(ushort)) { type3 = typeof(short); } else if (managedType == typeof(uint)) { type3 = typeof(int); } else if (managedType == typeof(ulong)) { type3 = typeof(long); } if (DataTypeStringParser.TryParse(str, type3, out obj3) & DataTypeStringParser.TryParse(str2, type3, out obj2)) { object obj4; object obj5; if (managedType == type3) { obj4 = obj2; obj5 = obj3; } else { PrimitiveTypeConverter converter = PrimitiveTypeConverter.Default; byte[] data = converter.Marshal(obj2); byte[] buffer2 = converter.Marshal(obj3); converter.UnmarshalPrimitive(managedType, data, 0, data.Length, out obj4); converter.UnmarshalPrimitive(managedType, buffer2, 0, buffer2.Length, out obj5); } string name = $"{baseType.Name} ({obj4}..{obj5})"; IDataType type4 = null; if (binder.TryResolveType(name, out type4)) { subRange = (ISubRangeType)type4; return(true); } } } subRange = null; return(false); }
private void CheckType(IDataType type, Type targetType) { List <IDataType> list = null; if (this._checkedTypesDict.TryGetValue(targetType, out list) && list.Contains(type)) { return; } switch (type.Category) { case DataTypeCategory.Primitive: case DataTypeCategory.Pointer: case DataTypeCategory.Reference: if (type.ByteSize > PrimitiveTypeConverter.MarshalSize(targetType)) { throw new MarshalException($"Source type '{type.Name}' is larger than target type '{targetType.Name}'!"); } goto TR_0007; case DataTypeCategory.Alias: { IAliasType type2 = (IAliasType)type; try { this.CheckType(type2.BaseType, targetType); } catch (MarshalException exception) { throw new MarshalException($"Cannot Marshal Alias '{type2.Name}' !", exception); } goto TR_0007; } case DataTypeCategory.Enum: { IEnumType type3 = (IEnumType)type; if (!targetType.IsEnum) { IManagedMappableType baseType = type3.BaseType as IManagedMappableType; bool flag = false; if (baseType == null) { throw new MarshalException($"Type '{targetType.Name}' is not an enum type or enum base type!"); } flag = baseType.ManagedType == targetType; } else { string[] names = type3.EnumValues.GetNames(); string[] strArray2 = Enum.GetNames(targetType); if (names.Length > strArray2.Length) { throw new MarshalException($"Enum Types '{type.Name}' and '{targetType.Name}' are not compatible!"); } StringComparer ordinalIgnoreCase = StringComparer.OrdinalIgnoreCase; string[] strArray3 = names; int index = 0; while (index < strArray3.Length) { string x = strArray3[index]; bool flag2 = false; string[] strArray4 = strArray2; int num5 = 0; while (true) { if (num5 < strArray4.Length) { string y = strArray4[num5]; if (ordinalIgnoreCase.Compare(x, y) != 0) { num5++; continue; } flag2 = true; } if (!flag2) { throw new MarshalException($"Enum Types '{type.Name}' and '{targetType.Name}' are not compatible!"); } index++; break; } } } goto TR_0007; } case DataTypeCategory.Array: { IArrayType type4 = (IArrayType)type; if (!targetType.IsArray) { throw new MarshalException($"Type '{targetType.Name}' is not an array type!"); } int arrayRank = targetType.GetArrayRank(); if (type4.Dimensions.Count != arrayRank) { throw new MarshalException($"Array Types '{type.Name}' and '{targetType.Name}' are not compatible!"); } Type elementType = targetType.GetElementType(); try { this.CheckType(type4.ElementType, elementType); } catch (MarshalException exception2) { throw new MarshalException($"Cannot Marshal Elements of Array '{type4.Name}'!", exception2); } goto TR_0007; } case DataTypeCategory.Struct: foreach (IMember member in ((IStructType)type).AllMembers) { PropertyInfo property = targetType.GetProperty(member.InstanceName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if (property != null) { this.CheckType(member.DataType, property.PropertyType); continue; } FieldInfo field = targetType.GetField(member.InstanceName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if (field != null) { Type fieldType = field.FieldType; try { this.CheckType(member.DataType, fieldType); } catch (MarshalException exception3) { IStructType type6; throw new MarshalException($"Cannot Marshal Member '{member.InstanceName}' of Source Struct '{type6.Name}' to field '{field.Name}' of target struct '{targetType.Name}'!", exception3); } } } goto TR_0007; case DataTypeCategory.SubRange: { ISubRangeType type9 = (ISubRangeType)type; try { this.CheckType(type9.BaseType, targetType); } catch (MarshalException exception4) { throw new MarshalException($"Cannot Marshal Subrange '{type9.Name}'!", exception4); } goto TR_0007; } case DataTypeCategory.String: break; case DataTypeCategory.Bitset: case DataTypeCategory.Union: goto TR_0007; default: throw new NotSupportedException(); } if (targetType != typeof(string)) { throw new MarshalException($"Type mismatch! Target Type '{type.Name}' is not a string (Marshalling AdsType '{targetType.Name}')!"); } TR_0007: if (list == null) { list = new List <IDataType>(); if (!this._checkedTypesDict.ContainsKey(targetType)) { this._checkedTypesDict.Add(targetType, list); } } list.Add(type); }