Exemplo n.º 1
0
        /// <summary>
        /// Sets the values of multiple properties.
        /// </summary>
        /// <param name="data">The keys and values of properties to set.</param>
        /// <returns>The new values of the properties. Actual values may not match requested values.</returns>
        public IDictionary <PropertyKey, object> SetProperties(DataFieldInfo[] data)
        {
            if (data == null || data.Length == 0)
            {
                throw new ArgumentNullException("data", "Data field array must not be null or empty.");
            }

            IPortableDeviceValues pdv = new PortableDeviceValues();

            for (int i = 0; i < data.Length; i++)
            {
                PropertyKey propKey = data[i].Key;
                object      value   = data[i].Value;
                if (value == null)
                {
                    throw new ArgumentNullException("data", String.Format("Data contains a null value at index {0}", i));
                }

                if (value is string)
                {
                    pdv.SetStringValue(ref propKey, (string)value);
                }
                else if (value is uint)
                {
                    pdv.SetUnsignedIntegerValue(ref propKey, (uint)value);
                }
                else if (value is int)
                {
                    pdv.SetSignedIntegerValue(ref propKey, (int)value);
                }
                else if (value is ulong)
                {
                    pdv.SetUnsignedLargeIntegerValue(ref propKey, (ulong)value);
                }
                else if (value is long)
                {
                    pdv.SetSignedLargeIntegerValue(ref propKey, (long)value);
                }
                else if (value is float || value is double)
                {
                    pdv.SetFloatValue(ref propKey, (float)value);
                }
                else if (value is bool)
                {
                    pdv.SetBoolValue(ref propKey, ((bool)value) ? 1 : 0);
                }
                else if (value is Guid)
                {
                    Guid guid = (Guid)value;
                    pdv.SetGuidValue(ref propKey, ref guid);
                }
                else if (value is byte[])
                {
                    byte[] buffer = (byte[])value;
                    pdv.SetBufferValue(ref propKey, buffer, (uint)buffer.Length);
                }
                else
                {
                    pdv.SetIUnknownValue(ref propKey, value);
                }
            }

            IPortableDeviceValues pdv2 = null;

            _iSensor.SetProperties(pdv, out pdv2);

            Dictionary <PropertyKey, object> results = new Dictionary <PropertyKey, object>();

            if (pdv2 == null)
            {
                return(results);
            }

            uint count = 0;

            pdv2.GetCount(ref count);

            for (uint i = 0; i < count; i++)
            {
                PropertyKey propKey = new PropertyKey();
                PROPVARIANT propVal = new PROPVARIANT();
                try
                {
                    pdv2.GetAt(i, ref propKey, out propVal);
                    results.Add(propKey, propVal.Value);
                }
                finally
                {
                    propVal.Clear();
                }
            }

            return(results);
        }