Beispiel #1
0
        public static string ToString(ITcAdsDataType dataType, object value)
        {
            AdsDatatypeId dataTypeId = dataType.BaseType.DataTypeId;

            if (dataTypeId == AdsDatatypeId.ADST_INT16)
            {
                return(EnumTypeConverter <short> .ToString(dataType, (short)value));
            }
            if (dataTypeId == AdsDatatypeId.ADST_INT32)
            {
                return(EnumTypeConverter <int> .ToString(dataType, (int)value));
            }
            switch (dataTypeId)
            {
            case AdsDatatypeId.ADST_INT8:
                return(EnumTypeConverter <sbyte> .ToString(dataType, (sbyte)value));

            case AdsDatatypeId.ADST_UINT8:
                return(EnumTypeConverter <byte> .ToString(dataType, (byte)value));

            case AdsDatatypeId.ADST_UINT16:
                return(EnumTypeConverter <ushort> .ToString(dataType, (ushort)value));

            case AdsDatatypeId.ADST_UINT32:
                return(EnumTypeConverter <uint> .ToString(dataType, (uint)value));

            case AdsDatatypeId.ADST_INT64:
                return(EnumTypeConverter <long> .ToString(dataType, (long)value));

            case AdsDatatypeId.ADST_UINT64:
                return(EnumTypeConverter <ulong> .ToString(dataType, (ulong)value));
            }
            throw new ArgumentException("Wrong data type!");
        }
        internal static string ToString(ITcAdsDataType enumType, T val)
        {
            string nameValue = null;

            if (!EnumTypeConverter <T> .TryGetName(enumType, val, out nameValue))
            {
                throw new ArgumentOutOfRangeException("val");
            }
            return(nameValue);
        }
        internal static T ToValue(ITcAdsDataType enumType, string stringValue)
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }
            if (stringValue == null)
            {
                throw new ArgumentNullException("value");
            }
            if (enumType.Category == DataTypeCategory.Enum)
            {
                throw new ArgumentException("Specified type is not an enum type!", "enumType");
            }
            T local = default(T);

            if (!EnumTypeConverter <T> .TryGetValue(enumType, stringValue, out local))
            {
                throw new ArgumentOutOfRangeException("stringValue");
            }
            return(local);
        }
        internal static bool TryGetName(ITcAdsDataType enumType, T value, out string nameValue)
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (enumType.Category != DataTypeCategory.Enum)
            {
                throw new ArgumentException("Specified type is not an enum type!", "enumType");
            }
            EnumValue <T> entry = EnumTypeConverter <T> .GetEntry(enumType, value);

            if (entry != null)
            {
                nameValue = entry.Name;
                return(true);
            }
            nameValue = null;
            return(false);
        }
        internal static bool TryGetValue(ITcAdsDataType enumType, string stringValue, out T value)
        {
            if (enumType == null)
            {
                throw new ArgumentNullException("enumType");
            }
            if (string.IsNullOrEmpty(stringValue))
            {
                throw new ArgumentOutOfRangeException("stringValue");
            }
            if (enumType.Category == DataTypeCategory.Enum)
            {
                throw new ArgumentException("Specified type is not an enum type!", "enumType");
            }
            EnumValue <T> entry = EnumTypeConverter <T> .GetEntry(enumType, stringValue);

            if (entry != null)
            {
                value = entry.Primitive;
                return(true);
            }
            value = default(T);
            return(false);
        }