示例#1
0
        /// <summary>
        /// Looks up the <see cref="IConvertFrom"/> for the target type.
        /// </summary>
        /// <param name="target">The type to lookup the converter for.</param>
        /// <returns>The converter for the specified type.</returns>
        public static IConvertFrom GetTypeConverter(Type target)
        {
            IConvertFrom converter = ConverterRegistry.GetConverter(target);

            if (converter == null)
            {
                throw new InvalidOperationException("No type converter defined for [" + target + "]");
            }
            return(converter);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="deviceInfo"></param>
        /// <returns></returns>
        public bool LoadValue(DeviceInfo deviceInfo)
        {
            deviceInfo.ThrowIfNull("deviceInfo");

            TPropertyType propertyType;

            Api.Buffer buffer;

            if (!LoadValue(deviceInfo, out propertyType, out buffer))
            {
                return(false);
            }

            using (buffer)
            {
                // If there's no buffer to work with, set a null value.
                if (buffer == null || buffer.Length == 0)
                {
                    Value = null;
                }
                else
                {
                    // Get the appropriate value converter.
                    var valueConverter = ConverterRegistry.GetConverter(propertyType);
                    if (valueConverter == null)
                    {
                        // When there's no converter, just copy the data as a byte array.
                        Value = buffer.GetBytes();
                    }
                    else
                    {
                        // When there is a converter, use it.
                        Value = valueConverter.BufferToObject(buffer);
                    }
                }

                // The type is deliberately set after the value. This allows for errors trying to convert the loaded
                // value, as the existing value and type will be left untouched.
                ValueType = propertyType;

                return(true);
            }
        }