Exemple #1
0
 internal Symbol(AdsSymbolEntry entry, IDataType type, ISymbol parent, ISymbolFactoryServices factoryServices) : this(entry, parent, factoryServices)
 {
     base.category         = type.Category;
     base.resolvedDataType = type;
     if ((base.attributes != null) && (base.attributes.Count >= 2))
     {
         ISubRangeType subRange = null;
         if (TryParseSubRange(type, base.attributes, factoryServices.Binder, out subRange))
         {
             base.category         = subRange.Category;
             base.resolvedDataType = subRange;
         }
     }
 }
Exemple #2
0
        internal static ISubRangeType Create(AdsDataTypeEntry entry, IDataTypeResolver resolver)
        {
            ISubRangeType type     = null;
            string        baseType = null;

            if (DataTypeStringParser.TryParseSubRange(entry.entryName, out baseType))
            {
                IDataType type2 = null;
                if (resolver.TryResolveType(baseType, out type2))
                {
                    type = Create(entry.entryName, type2);
                }
            }
            return(type);
        }
Exemple #3
0
        private static ISubRangeType Create(string name, IDataType baseType)
        {
            ISubRangeType type = null;
            string        str;
            Type          managedType = ((IManagedMappableType)baseType).ManagedType;

            if (managedType == typeof(sbyte))
            {
                sbyte num;
                sbyte num2;
                DataTypeStringParser.TryParseSubRange <sbyte>(name, out str, out num, out num2);
                type = new SubRangeType <sbyte>(name, baseType.Name, 1, num, num2);
            }
            else if (managedType == typeof(byte))
            {
                byte num3;
                byte num4;
                DataTypeStringParser.TryParseSubRange <byte>(name, out str, out num3, out num4);
                type = new SubRangeType <byte>(name, baseType.Name, 1, num3, num4);
            }
            else if (managedType == typeof(short))
            {
                short num5;
                short num6;
                DataTypeStringParser.TryParseSubRange <short>(name, out str, out num5, out num6);
                type = new SubRangeType <short>(name, baseType.Name, 2, num5, num6);
            }
            else if (managedType == typeof(ushort))
            {
                ushort num7;
                ushort num8;
                DataTypeStringParser.TryParseSubRange <ushort>(name, out str, out num7, out num8);
                type = new SubRangeType <ushort>(name, baseType.Name, 2, num7, num8);
            }
            else if (managedType == typeof(int))
            {
                int num9;
                int num10;
                DataTypeStringParser.TryParseSubRange <int>(name, out str, out num9, out num10);
                type = new SubRangeType <int>(name, baseType.Name, 4, num9, num10);
            }
            else if (managedType == typeof(uint))
            {
                uint num11;
                uint num12;
                DataTypeStringParser.TryParseSubRange <uint>(name, out str, out num11, out num12);
                type = new SubRangeType <uint>(name, baseType.Name, 4, num11, num12);
            }
            else if (managedType == typeof(long))
            {
                long num13;
                long num14;
                DataTypeStringParser.TryParseSubRange <long>(name, out str, out num13, out num14);
                type = new SubRangeType <long>(name, baseType.Name, 8, num13, num14);
            }
            else
            {
                ulong num15;
                ulong num16;
                if (managedType != typeof(ulong))
                {
                    throw new AdsException($"Could not create range type '{name}'");
                }
                DataTypeStringParser.TryParseSubRange <ulong>(name, out str, out num15, out num16);
                type = new SubRangeType <ulong>(name, baseType.Name, 8, num15, num16);
            }
            return(type);
        }
        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);
        }
Exemple #5
0
        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);
        }