예제 #1
0
        public AdsErrorCode WriteAny(uint indexGroup, uint indexOffset, object value, bool throwAdsException)
        {
            Type type = value.GetType();

            if (type.IsPrimitive)
            {
                if (type == typeof(bool))
                {
                    return(base.Write(indexGroup, indexOffset, (bool)value, throwAdsException));
                }
                if (type == typeof(int))
                {
                    return(base.Write(indexGroup, indexOffset, (int)value, throwAdsException));
                }
                if (type == typeof(short))
                {
                    return(base.Write(indexGroup, indexOffset, (short)value, throwAdsException));
                }
                if (type == typeof(byte))
                {
                    return(base.Write(indexGroup, indexOffset, (byte)value, throwAdsException));
                }
                if (type == typeof(float))
                {
                    return(base.Write(indexGroup, indexOffset, (float)value, throwAdsException));
                }
                if (type == typeof(double))
                {
                    return(base.Write(indexGroup, indexOffset, (double)value, throwAdsException));
                }
                if (type == typeof(long))
                {
                    return(base.Write(indexGroup, indexOffset, (long)value, throwAdsException));
                }
                if (type == typeof(uint))
                {
                    return(base.Write(indexGroup, indexOffset, (uint)value, throwAdsException));
                }
                if (type == typeof(ushort))
                {
                    return(base.Write(indexGroup, indexOffset, (ushort)value, throwAdsException));
                }
                if (type == typeof(ulong))
                {
                    return(base.Write(indexGroup, indexOffset, (ulong)value, throwAdsException));
                }
                if (type != typeof(sbyte))
                {
                    throw new ArgumentException("Unable to marshal object.", "value");
                }
                return(base.Write(indexGroup, indexOffset, (sbyte)value, throwAdsException));
            }
            if (!type.IsArray)
            {
                if (type == typeof(string))
                {
                    string str = (string)value;
                    throw new ArgumentException("Use overload WriteAnyString(uint indexGroup, uint indexOffset, string value, int characters)) for strings.", "value");
                }
                if (type == typeof(DateTime))
                {
                    uint num = PlcOpenDateConverterBase.ToTicks((DateTime)value);
                    return(base.Write(indexGroup, indexOffset, num, throwAdsException));
                }
                if (type != typeof(TimeSpan))
                {
                    return((type != typeof(TIME)) ? ((type != typeof(LTIME)) ? ((type != typeof(TOD)) ? ((type != typeof(DATE)) ? ((type != typeof(DT)) ? base.Write(indexGroup, indexOffset, value, throwAdsException) : base.Write(indexGroup, indexOffset, ((DT)value).Ticks, throwAdsException)) : base.Write(indexGroup, indexOffset, ((DATE)value).Ticks, throwAdsException)) : base.Write(indexGroup, indexOffset, ((TOD)value).Ticks, throwAdsException)) : base.Write(indexGroup, indexOffset, ((LTIME)value).Ticks, throwAdsException)) : base.Write(indexGroup, indexOffset, ((TIME)value).Ticks, throwAdsException));
                }
                uint val = PlcOpenTimeConverter.ToMilliseconds((TimeSpan)value);
                return(base.Write(indexGroup, indexOffset, val, throwAdsException));
            }
            Type elementType = type.GetElementType();

            if (!elementType.IsPrimitive)
            {
                if (type == typeof(string[]))
                {
                    throw new ArgumentException("Use overload WriteString(uint indexGroup, uint indexOffset, string value, int characters)) for strings.", "value");
                }
                if (elementType.IsArray || (type.GetArrayRank() != 1))
                {
                    throw new ArgumentException("Unable to marshal object.", "value");
                }
                return(base.WriteArrayOfStruct(indexGroup, indexOffset, value, throwAdsException));
            }
            if (elementType == typeof(bool))
            {
                return(base.WriteArrayOfBoolean(indexGroup, indexOffset, (Array)value, throwAdsException));
            }
            if (elementType == typeof(int))
            {
                return(base.WriteArrayOfInt32(indexGroup, indexOffset, (Array)value, throwAdsException));
            }
            if (elementType == typeof(short))
            {
                return(base.WriteArrayOfInt16(indexGroup, indexOffset, (Array)value, throwAdsException));
            }
            if (elementType == typeof(byte))
            {
                return(base.WriteArrayOfUInt8(indexGroup, indexOffset, (Array)value, throwAdsException));
            }
            if (elementType == typeof(float))
            {
                return(base.WriteArrayOfReal32(indexGroup, indexOffset, (Array)value, throwAdsException));
            }
            if (elementType == typeof(double))
            {
                return(base.WriteArrayOfReal64(indexGroup, indexOffset, (Array)value, throwAdsException));
            }
            if (elementType == typeof(long))
            {
                return(base.WriteArrayOfInt64(indexGroup, indexOffset, (Array)value, throwAdsException));
            }
            if (elementType == typeof(uint))
            {
                return(base.WriteArrayOfUInt32(indexGroup, indexOffset, (Array)value, throwAdsException));
            }
            if (elementType == typeof(ushort))
            {
                return(base.WriteArrayOfUInt16(indexGroup, indexOffset, (Array)value, throwAdsException));
            }
            if (elementType != typeof(sbyte))
            {
                throw new ArgumentException("Unable to marshal object.", "value");
            }
            return(base.WriteArrayOfInt8(indexGroup, indexOffset, (Array)value, throwAdsException));
        }
예제 #2
0
        public AdsErrorCode ReadAny(uint indexGroup, uint indexOffset, Type type, bool throwAdsException, out object value)
        {
            AdsErrorCode code;

            if (!type.IsPrimitive)
            {
                if (type == typeof(string))
                {
                    throw new ArgumentException("Use overload ReadAnyString(uint indexGroup, uint indexOffset,  Type type, int characters) for strings.", "type");
                }
                if (type.IsArray)
                {
                    throw new ArgumentException("Use overload ReadAny(uint indexGroup, uint indexOffset,  Type type, int[] args) for arrays.", "type");
                }
                if (type == typeof(TimeSpan))
                {
                    uint milliseconds = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = PlcOpenTimeConverter.MillisecondsToTimeSpan(milliseconds);
                }
                else if (type == typeof(DateTime))
                {
                    uint dateValue = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = PlcOpenDateConverterBase.ToDateTime(dateValue);
                }
                else if (type == typeof(TIME))
                {
                    uint timeValue = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = new TIME(timeValue);
                }
                else if (type == typeof(LTIME))
                {
                    ulong timeValue = base.ReadUInt64(indexGroup, indexOffset, throwAdsException, out code);
                    value = new LTIME(timeValue);
                }
                else if (type == typeof(TOD))
                {
                    uint time = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = new TOD(time);
                }
                else if (type == typeof(DATE))
                {
                    uint dateValue = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = new DATE(dateValue);
                }
                else if (type != typeof(DT))
                {
                    value = base.ReadStruct(indexGroup, indexOffset, type, throwAdsException, out code);
                }
                else
                {
                    uint dateValue = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
                    value = new DT(dateValue);
                }
            }
            else if (type == typeof(bool))
            {
                value = base.ReadBoolean(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(int))
            {
                value = base.ReadInt32(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(short))
            {
                value = base.ReadInt16(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(byte))
            {
                value = base.ReadUInt8(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(float))
            {
                value = base.ReadReal32(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(double))
            {
                value = base.ReadReal64(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(long))
            {
                value = base.ReadInt64(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(uint))
            {
                value = base.ReadUInt32(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(ushort))
            {
                value = base.ReadUInt16(indexGroup, indexOffset, throwAdsException, out code);
            }
            else if (type == typeof(ulong))
            {
                value = base.ReadUInt64(indexGroup, indexOffset, throwAdsException, out code);
            }
            else
            {
                if (type != typeof(sbyte))
                {
                    throw new ArgumentException("Unable to marshal type.", "type");
                }
                value = base.ReadInt8(indexGroup, indexOffset, throwAdsException, out code);
            }
            return(code);
        }