I look at your requests, and one is better than the second, but the last time I have to rewrite in order to better understand the solution. And this solution missed the long, if something else stack, and replaced its foreach with an enumeration of types, where we can implement all the types that we need. I prefer using dynamic, but it can also be used.
The main function GetValueByValidating return value, if the type is defined and possible, otherwise returns false Look niranjan-kala , this is your main function after overwriting.
/// /// Enum of wanted types /// public enum Types { [ExtendetFlags("int")] INT, [ExtendetFlags("decimal")] DECIMAL, [ExtendetFlags("double")] DOUBLE, [ExtendetFlags("real")] REAL, [ExtendetFlags("string")] STRING, [ExtendetFlags("object")] OBJECT, [ExtendetFlags("null")] NULLABLE } /// /// Cycle by types when in enum exist string reference on type (helper) /// /// /// public static Types GetCurrentType(string container) { foreach (Types t in Enum.GetValues(typeof(Types))) { if (container.Contains(t.GetFlagValue())) { return t; } } return Types.NULLABLE; } /// /// Return object converted to type /// /// /// /// public static object GetValueByValidating(string strCurrentDatatype, object valueObj) { var _value = valueObj != null ? valueObj : null; try { Types _current = _value != null ? GetCurrentType(strCurrentDatatype.ToLower()) : Types.NULLABLE; switch (_current) { case Types.INT: valueObj = Convert.ToInt32(valueObj); break; case Types.DECIMAL: valueObj = Convert.ToDecimal(valueObj); break; case Types.DOUBLE: valueObj = Convert.ToDouble(valueObj); break; case Types.REAL: valueObj = Convert.ToDouble(valueObj); break; case Types.STRING: valueObj = Convert.ToString(valueObj); break; case Types.OBJECT: break; case Types.NULLABLE: throw new InvalidCastException("Type not handled before selecting, function crashed by retype var."); } } catch (InvalidCastException ex) { Log.WriteException(ex); valueObj = false; } return valueObj; }
Michael gordon
source share