示例#1
0
 internal EnumValueCollection(AdsDatatypeId typeId, AdsEnumInfoEntry[] coll)
 {
     this._list          = new List <IEnumValue>();
     this._nameValueDict = new Dictionary <string, IEnumValue>(StringComparer.OrdinalIgnoreCase);
     if (coll == null)
     {
         throw new ArgumentNullException("coll");
     }
     foreach (AdsEnumInfoEntry entry in coll)
     {
         IEnumValue item = EnumValueFactory.Create(typeId, entry);
         this.Add(item);
     }
 }
        internal int Marshal(IDataType type, Encoding encoding, object val, byte[] bValue, int offset)
        {
            int  num   = 0;
            Type type2 = val.GetType();

            switch (type.Category)
            {
            case DataTypeCategory.Primitive:
            case DataTypeCategory.SubRange:
            case DataTypeCategory.String:
                num     = this._internalConverter.TypeMarshaller.Marshal(type, encoding, val, bValue, offset);
                offset += num;
                return(num);

            case DataTypeCategory.Alias:
            {
                IAliasType type3 = (IAliasType)type;
                return(this.Marshal(type3.BaseType, encoding, val, bValue, offset));
            }

            case DataTypeCategory.Enum:
            {
                IEnumType enumType = (IEnumType)type;
                if (!type2.IsEnum)
                {
                    throw new ArgumentException("Type is not an enum type!", "type");
                }
                IEnumValue value2 = EnumValueFactory.Create(enumType, val);
                Array.Copy(value2.RawValue, 0, bValue, offset, value2.RawValue.Length);
                offset += value2.RawValue.Length;
                return(num + value2.RawValue.Length);
            }

            case DataTypeCategory.Array:
                break;

            case DataTypeCategory.Struct:
                foreach (IMember member in ((IStructType)type).AllMembers)
                {
                    PropertyInfo property = type2.GetProperty(member.InstanceName, BindingFlags.Public | BindingFlags.IgnoreCase);
                    if (((property != null) && (property.GetGetMethod() != null)) && (property.GetSetMethod() != null))
                    {
                        num += this.Marshal(member, property.GetValue(type2, new object[0]), bValue, offset);
                        continue;
                    }
                    FieldInfo field = type2.GetField(member.InstanceName, BindingFlags.Public | BindingFlags.IgnoreCase);
                    if (field == null)
                    {
                        throw new ArgumentException("Struct member not found!", "type");
                    }
                    num += this.Marshal(member, field.GetValue(type2), bValue, offset);
                }
                return(num);

            default:
                throw new NotSupportedException();
            }
            IArrayType type6     = (IArrayType)type;
            int        arrayRank = type2.GetArrayRank();
            Array      array     = (Array)val;

            int[] numArray  = new int[arrayRank];
            int[] numArray2 = new int[arrayRank];
            int[] numArray3 = new int[arrayRank];
            int[] numArray4 = new int[arrayRank];
            int   index     = 0;

            while (true)
            {
                if (index >= arrayRank)
                {
                    int position = 0;
                    while (position < type6.Dimensions.ElementCount)
                    {
                        int[] indicesOfPosition = ((ArrayType)type6).GetIndicesOfPosition(position);
                        int[] indices           = new int[indicesOfPosition.Length];
                        int   num6 = 0;
                        while (true)
                        {
                            if (num6 >= indicesOfPosition.Length)
                            {
                                object obj4          = array.GetValue(indices);
                                int    elementOffset = ((ArrayType)type6).GetElementOffset(indices);
                                num += this.Marshal(type, encoding, obj4, bValue, offset + elementOffset);
                                position++;
                                break;
                            }
                            int num7 = numArray[num6] - numArray3[num6];
                            indices[num6] = indicesOfPosition[num6] + num7;
                            num6++;
                        }
                    }
                    break;
                }
                numArray[index]  = array.GetLowerBound(index);
                numArray2[index] = array.GetUpperBound(index);
                numArray3[index] = ((Dimension)type6.Dimensions[index]).LowerBound;
                numArray4[index] = ((Dimension)type6.Dimensions[index]).UpperBound;
                index++;
            }
            return(num);
        }
        private void initializeInstanceValue(IDataType type, Encoding encoding, object targetInstance, Type targetType, object targetMember, byte[] data, int offset)
        {
            object obj2 = null;

            switch (type.Category)
            {
            case DataTypeCategory.Primitive:
            case DataTypeCategory.String:
                this._internalConverter.TypeMarshaller.Unmarshal(type, encoding, data, offset, out obj2);
                this.initializeInstanceValue(targetInstance, targetMember, obj2);
                return;

            case DataTypeCategory.Alias:
            {
                IAliasType type2 = (IAliasType)type;
                this.initializeInstanceValue(type2.BaseType, encoding, targetInstance, targetType, targetMember, data, offset);
                return;
            }

            case DataTypeCategory.Enum:
            {
                IEnumType  enumType    = (IEnumType)type;
                Type       managedType = ((IManagedMappableType)enumType.BaseType).ManagedType;
                IEnumValue value2      = EnumValueFactory.Create(enumType, data, offset);
                if (!targetType.IsEnum)
                {
                    throw new ArgumentException("Type is not an enum type or enum base type!", "type");
                }
                obj2 = Enum.Parse(targetType, value2.ToString(), true);
                this.initializeInstanceValue(targetInstance, targetMember, obj2);
                return;
            }

            case DataTypeCategory.Array:
            {
                IArrayType type6       = (IArrayType)type;
                int        arrayRank   = targetType.GetArrayRank();
                Array      array       = (Array)targetInstance;
                int[]      numArray    = new int[arrayRank];
                int[]      numArray2   = new int[arrayRank];
                int[]      lowerBounds = type6.Dimensions.LowerBounds;
                int[]      upperBounds = type6.Dimensions.UpperBounds;
                for (int i = 0; i < arrayRank; i++)
                {
                    numArray[i]  = array.GetLowerBound(i);
                    numArray2[i] = array.GetUpperBound(i);
                }
                int position = 0;
                while (position < type6.Dimensions.ElementCount)
                {
                    int[] indicesOfPosition = ((ArrayType)type6).GetIndicesOfPosition(position);
                    int[] indices           = new int[indicesOfPosition.Length];
                    int   index             = 0;
                    while (true)
                    {
                        if (index >= indicesOfPosition.Length)
                        {
                            object obj3          = array.GetValue(indices);
                            int    elementOffset = ((ArrayType)type6).GetElementOffset(indicesOfPosition);
                            if (obj3 != null)
                            {
                                this.initializeInstanceValue(type6.ElementType, encoding, obj3, obj3.GetType(), indices, data, elementOffset);
                            }
                            else
                            {
                                TwinCAT.Ads.Module.Trace.TraceError("Failed to fill array element!");
                            }
                            position++;
                            break;
                        }
                        int num6 = numArray[index] - lowerBounds[index];
                        indices[index] = indicesOfPosition[index] + num6;
                        index++;
                    }
                }
                return;
            }

            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) && (property.GetGetMethod() != null)) && (property.GetSetMethod() != null))
                    {
                        this.initializeInstanceValue(member.DataType, encoding, targetInstance, targetType, property, data, offset + member.ByteOffset);
                        continue;
                    }
                    FieldInfo field = targetType.GetField(member.InstanceName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    if (field != null)
                    {
                        this.initializeInstanceValue(member.DataType, encoding, targetInstance, targetType, field, data, offset + member.ByteOffset);
                    }
                    else
                    {
                        object[] args = new object[] { member.InstanceName, targetType.ToString() };
                        TwinCAT.Ads.Module.Trace.TraceWarning("Struct member '{0}' not found within {1}!", args);
                    }
                }
                break;

            case DataTypeCategory.SubRange:
            case DataTypeCategory.Bitset:
                break;

            case DataTypeCategory.Pointer:
            case DataTypeCategory.Reference:
            {
                int byteSize = ((IReferenceType)type).ByteSize;
                return;
            }

            default:
                throw new NotSupportedException();
            }
        }