public static T[] ConvertObject2Array <T>(object arg, T def_value) where T : new() { try { if (arg is int || arg is double || arg is T) { T[] ret = new T[1]; ret[0] = (T)arg; return(ret); } else if (arg is object[, ]) { int row = ((object[, ])arg).GetLength(0); int col = ((object[, ])arg).GetLength(1); T[] ret = new T[row * col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { ret[i * col + j] = EQConverter.ConvertObject <T>(((object[, ])arg)[i, j], def_value); } } return(ret); } } catch (Exception e) { throw e; } T[] retArr = new T[1]; retArr[0] = def_value; return(retArr); }
public static T ConvertObject <T>(Object obj, Object defaultValue) { try { // pass through if (obj.GetType() == typeof(T)) { return((T)(Object)obj); } if (typeof(T) == typeof(DateTime)) // To DateTime { if (obj is EliteQuant.Date) { return((T)(Object)DateTime.FromOADate(((EliteQuant.Date)obj).serialNumber())); } else if (obj is String) { return((T)(Object)DateTime.ParseExact((String)obj, "M/d/yyyy", null)); } else if (obj is double) { return((T)(Object)DateTime.FromOADate((double)obj)); } else if (obj is object[, ]) { return((T)(Object)EliteQuant.EQConverter.ConvertObject2Array(obj, DateTime.MinValue)[0]); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to System.DateTime"); } } else if (typeof(T) == typeof(EliteQuant.Date)) // to QuanLib.Date { if (obj is DateTime) { EliteQuant.Date d = new EliteQuant.Date(Convert.ToInt32(((DateTime)obj).ToOADate())); return((T)(Object)d); } else if (obj is double) { EliteQuant.Date d = new EliteQuant.Date(Convert.ToInt32((double)obj)); return((T)(Object)d); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to EliteQuant.Date"); } } else if (typeof(T) == typeof(EliteQuant.Calendar)) // to EliteQuant.Calendar { if (obj is String) { string c = (string)obj; return((T)(Object)(EliteQuant.EQConverter.ConvertStringToCalendar(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to Calendar"); } } else if (typeof(T) == typeof(EliteQuant.DayCounter)) // to DayCounter { if (obj is string) { string c = (string)obj; return((T)(object)(EliteQuant.EQConverter.ConvertStringToDayCounter(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to DayCounter"); } } else if (typeof(T) == typeof(EliteQuant.BusinessDayConvention)) // to bdc { if (obj is string) { string c = (string)obj; return((T)(object)(EliteQuant.EQConverter.ConvertStringToBDC(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to BusinessDayConvention"); } } else if (typeof(T) == typeof(EliteQuant.DateGeneration.Rule)) // to rule { if (obj is string) { string c = (string)obj; return((T)(object)(EliteQuant.EQConverter.ConvertStringToDGRule(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to DateGenerationRule"); } } else if (typeof(T) == typeof(EliteQuant.Frequency)) // to rule { if (obj is string) { string c = (string)obj; return((T)(object)(EliteQuant.EQConverter.ConvertStringToFrequency(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to Frequency"); } } else if (typeof(T) == typeof(EliteQuant.Compounding)) // to rule { if (obj is string) { string c = (string)obj; return((T)(object)(EliteQuant.EQConverter.ConvertStringToCompounding(c))); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to Compounding"); } } else if (typeof(T) == typeof(EliteQuant.Period)) { if (obj is String) // String (2D, 3M, 10Y) { return((T)(Object)(EliteQuant.EQConverter.ConvertStringToPeriod((string)obj))); } else if (obj is EliteQuant.Frequency) // Monthly, Annual { string[] freqNames = Enum.GetNames(typeof(EliteQuant.Frequency)); StringCollection sc = new StringCollection(); sc.AddRange(freqNames); if (sc.Contains(obj as string)) { EliteQuant.Frequency frq = (EliteQuant.Frequency)Enum.Parse(typeof(EliteQuant.Frequency), obj as string); return((T)(Object) new EliteQuant.Period(frq)); } else { return((T)(Object) new EliteQuant.Period((EliteQuant.Frequency)obj)); } } else if (obj is int) { return((T)(Object) new EliteQuant.Period((int)obj, EliteQuant.TimeUnit.Years)); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to EliteQuant.Period"); } } else if (typeof(T) == typeof(DateTime[])) { if (obj is object[, ] || obj is DateTime || obj is double) { return((T)(Object)EQConverter.ConvertObject2Array(obj, DateTime.MinValue)); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to DateTime[]"); } } else if (typeof(T) == typeof(EliteQuant.Period[])) { if (obj is object[, ] || obj is String) { return((T)(Object)EQConverter.ConvertObject2Array(obj, new EliteQuant.Period())); } else { throw new ArgumentOutOfRangeException("Unknown type to convert to EliteQuant.Period[]"); } } else if (typeof(T) == typeof(double)) { if (obj is String) { double amount = 0.0; double.TryParse((String)obj, out amount); return((T)(Object)amount); } } else if (typeof(T) == typeof(double[])) { if (obj is object[, ] || obj is double) { double def = (defaultValue is double) ? (double)defaultValue : 0.0; return((T)(Object)EQConverter.ConvertObject2Array <double>(obj, def)); } } else if (typeof(T).IsEnum) { if (obj is String) { if (EQEnum.EnumDictionary.ContainsKey(obj as string)) { obj = EQEnum.EnumDictionary[obj as string]; } return((T)Enum.Parse(typeof(T), (string)obj, true)); } } else if (typeof(T) == typeof(String[])) { if (obj is object[, ] || obj is string) { StringCollection tmp = EQConverter.ConvertObject2StringCollection(obj); String[] ret = new String[tmp.Count]; tmp.CopyTo(ret, 0); return((T)(Object)ret); } } else if (typeof(T) == typeof(StringCollection)) { if (obj is object[, ] || obj is string || obj is double) { return((T)(Object)EQConverter.ConvertObject2StringCollection(obj)); } } else if (typeof(T) == typeof(bool[])) { if (obj is object[, ] || obj is bool) { bool def = (defaultValue is bool) ? (bool)defaultValue : false; return((T)(Object)EQConverter.ConvertObject2Array <bool>(obj, def)); } } else if (typeof(T) == typeof(int[])) { if (obj is object[, ] || obj is double) { double def = (defaultValue is int) ? Convert.ToDouble(defaultValue) : 0.0; double[] tmp = EQConverter.ConvertObject2Array <double>(obj, def); int[] ret = new int[tmp.Length]; for (int i = 0; i < ret.Length; i++) { ret[i] = (int)tmp[i]; } return((T)(Object)ret); } } } catch (System.Exception e) { if (defaultValue != null) { return(DefaultValue <T>(defaultValue)); } else { throw new Exception(" EQConverter failed: " + e.Message); } } return(DefaultValue <T>(defaultValue)); }