using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace IoTDSProviderSample { public partial class ValueInputControll : UserControl { private class ValueTypeListItem { public string VariantType { get; private set; } public Type ManagedType { get; private set; } public ValueTypeListItem(string variantType, Type managedType) { VariantType = variantType; ManagedType = managedType; } } private readonly ValueTypeListItem[] VALUE_TYPES = { new ValueTypeListItem("VT_EMPTY", null), new ValueTypeListItem("VT_NULL", typeof(DBNull)), new ValueTypeListItem("VT_I1", typeof(sbyte)), new ValueTypeListItem("VT_UI1", typeof(byte)), new ValueTypeListItem("VT_I2", typeof(short)), new ValueTypeListItem("VT_UI2", typeof(ushort)), new ValueTypeListItem("VT_I4", typeof(int)), new ValueTypeListItem("VT_UI4", typeof(uint)), new ValueTypeListItem("VT_I8", typeof(long)), new ValueTypeListItem("VT_UI8", typeof(ulong)), new ValueTypeListItem("VT_R4", typeof(float)), new ValueTypeListItem("VT_R8", typeof(double)), new ValueTypeListItem("VT_CY", typeof(decimal)), new ValueTypeListItem("VT_DATE", typeof(DateTime)), new ValueTypeListItem("VT_BSTR", typeof(string)), new ValueTypeListItem("VT_I1 | VT_ARRAY", typeof(sbyte[])), new ValueTypeListItem("VT_UI1 | VT_ARRAY", typeof(byte[])), new ValueTypeListItem("VT_I2 | VT_ARRAY", typeof(short[])), new ValueTypeListItem("VT_UI2 | VT_ARRAY", typeof(ushort[])), new ValueTypeListItem("VT_I4 | VT_ARRAY", typeof(int[])), new ValueTypeListItem("VT_UI4 | VT_ARRAY", typeof(uint[])), new ValueTypeListItem("VT_I8 | VT_ARRAY", typeof(long[])), new ValueTypeListItem("VT_UI8 | VT_ARRAY", typeof(ulong[])), new ValueTypeListItem("VT_R4 | VT_ARRAY", typeof(float[])), new ValueTypeListItem("VT_R8 | VT_ARRAY", typeof(double[])), new ValueTypeListItem("VT_CY | VT_ARRAY", typeof(decimal[])), new ValueTypeListItem("VT_DATE | VT_ARRAY", typeof(DateTime[])), new ValueTypeListItem("VT_BSTR | VT_ARRAY", typeof(string[])), new ValueTypeListItem("VT_VARIANT | VT_ARRAY", typeof(object[])), }; public ValueInputControll() { InitializeComponent(); cmbValueType.DisplayMember = "VariantType"; cmbValueType.ValueMember = "ManagedType"; cmbValueType.DataSource = VALUE_TYPES; cmbValueType.SelectedIndex = 0; } public object Value { get { var type = cmbValueType.SelectedValue as Type; if (type == null) { return null; } else if (type == typeof(DBNull)) { return DBNull.Value; } else if (type.IsArray) { var elementType = type.GetElementType(); var tokens = txtValue.Text.Split(','); var arr = Array.CreateInstance(elementType, tokens.Length); for (int i = 0; i < tokens.Length; i++) { arr.SetValue(Convert.ChangeType(tokens[i], elementType), i); } return arr; } else { return Convert.ChangeType(txtValue.Text, type); } } set { if (value == null) { cmbValueType.SelectedIndex = 0; txtValue.Text = ""; } else { var type = value.GetType(); cmbValueType.SelectedValue = type; if (value is Array) { var elements = new List(); foreach (var val in (Array)value) { elements.Add(Convert.ToString(val)); } txtValue.Text = string.Join(",", elements); } else { txtValue.Text = Convert.ToString(value); } } } } } }