static public object ChangeType(object value, DataType type, CultureInfo cultureInfo)
        {
            object ret;

            if (type == DataType.OctetString)
            {
                if (value is byte[])
                {
                    ret = value;
                }
                else
                {
                    ret = GXDLMSTranslator.HexToBytes((string)value);
                }
            }
            else if (type == DataType.DateTime)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXDateTime((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Date)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXDate((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Time)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXTime((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Enum)
            {
                if (value is GXEnum)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXEnum((byte)Convert.ChangeType(value, typeof(byte)));
                }
            }
            else if (type == DataType.Structure || type == DataType.Array)
            {
                ret = GXDLMSTranslator.XmlToValue((string)value);
            }
            else if (type == DataType.BitString)
            {
                ret = new GXBitString((string)value);
            }
            else if (type == DataType.None && value is string && string.IsNullOrEmpty((string)value))
            {
                ret = null;
            }
            else
            {
                ret = Convert.ChangeType(value, GXDLMSConverter.GetDataType(type), cultureInfo);
            }
            return(ret);
        }
示例#2
0
        static public object ChangeType(object value, DataType type, CultureInfo cultureInfo)
        {
            object ret;

            if (type == DataType.OctetString)
            {
                if (value is byte[])
                {
                    ret = value;
                }
                else
                {
                    ret = GXDLMSTranslator.HexToBytes((string)value);
                }
            }
            else if (type == DataType.DateTime)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXDateTime((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Date)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXDate((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Time)
            {
                if (value is GXDateTime)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXTime((string)value, CultureInfo.InvariantCulture);
                }
            }
            else if (type == DataType.Enum)
            {
                if (value is GXEnum)
                {
                    ret = value;
                }
                else
                {
                    ret = new GXEnum((byte)Convert.ChangeType(value, typeof(byte)));
                }
            }
            else
            {
                ret = Convert.ChangeType(value, GXDLMSConverter.GetDataType(type));
            }
            return(ret);
        }