/// <summary> /// Attempts to convert the specified property from its string representation in the loaded dictionary /// to its typed version. Sets the value to null if conversion fails. Intended for use with System.Drawing.Point /// </summary> /// <param name="properties"></param> /// <param name="property"></param> /// <param name="converter"></param> private static void ConvertSnapshotPoint(Dictionary <SnapshotMetaPropertyName, object> properties, SnapshotMetaPropertyName property, System.Drawing.PointConverter converter) { object strVal = null; if (properties?.TryGetValue(property, out strVal) == true) { try { properties[property] = converter.ConvertFromString((string)strVal); } catch (NotSupportedException) { properties[property] = null; } } }
private static void _SetObjValue(object obj, System.Data.DataRow row, System.Reflection.PropertyInfo p, string parentProName) { string colName = p.Name; if (parentProName != null) { colName += "-" + parentProName; } if (row.Table.Columns.IndexOf(colName) < 0) { return; } //if (p.PropertyType.IsEnum) //{ // p.SetValue(obj, System.Convert.ToInt32(row[colName]), null); //} //else //{ // var converter = System.ComponentModel.TypeDescriptor.GetConverter(p.PropertyType); // object cov = converter.ConvertFromString(row[colName].ToString()); // p.SetValue(obj, cov, null); //} if (p.PropertyType == typeof(string)) { p.SetValue(obj, row[colName].ToString(), null); } else if (p.PropertyType == typeof(Guid)) { p.SetValue(obj, Guid.Parse(row[colName].ToString()), null); } else if (p.PropertyType == typeof(SByte)) { p.SetValue(obj, (SByte)System.Convert.ToInt32(row[colName]), null); } else if (p.PropertyType == typeof(Int16)) { p.SetValue(obj, (Int16)System.Convert.ToInt32(row[colName]), null); } else if (p.PropertyType == typeof(Int32)) { p.SetValue(obj, System.Convert.ToInt32(row[colName]), null); } else if (p.PropertyType == typeof(System.DateTime)) { p.SetValue(obj, DateTime.Parse(row[colName].ToString()), null); } else if (p.PropertyType == typeof(Int64)) { p.SetValue(obj, System.Convert.ToInt64(row[colName]), null); } else if (p.PropertyType == typeof(Byte)) { p.SetValue(obj, (Byte)System.Convert.ToUInt32(row[colName]), null); } else if (p.PropertyType == typeof(UInt16)) { p.SetValue(obj, (UInt16)System.Convert.ToUInt32(row[colName]), null); } else if (p.PropertyType == typeof(UInt32)) { p.SetValue(obj, System.Convert.ToUInt32(row[colName]), null); } else if (p.PropertyType == typeof(UInt64)) { p.SetValue(obj, System.Convert.ToUInt64(row[colName]), null); } else if (p.PropertyType == typeof(Single)) { p.SetValue(obj, System.Convert.ToSingle(row[colName]), null); } else if (p.PropertyType == typeof(Double)) { p.SetValue(obj, System.Convert.ToDouble(row[colName]), null); } else if (p.PropertyType == typeof(bool)) { string b = row[colName].ToString(); if (b.ToLower() == "true") { p.SetValue(obj, true, null); } else { p.SetValue(obj, false, null); } } else if (p.PropertyType == typeof(System.Drawing.PointF)) { var converter = new System.Drawing.PointConverter(); System.Drawing.Point pt = (System.Drawing.Point)converter.ConvertFromString(row[colName].ToString()); p.SetValue(obj, new System.Drawing.PointF(pt.X, pt.Y), null); } else if (p.PropertyType.IsEnum) { p.SetValue(obj, System.Convert.ToInt32(row[colName]), null); } }