public To ConvertData <To, From>(From obj, string Delim = "") { SupIC FromT = SupIC.Nullable, ToT = SupIC.Nullable; Func <Type, Type, bool> getInterface = ((T, t) => t.IsAssignableFrom(T) ? true : false); if (getInterface(typeof(From), typeof(ICollection))) { if (getInterface(typeof(From), typeof(Array))) { FromT = SupIC.IArr; } else if (getInterface(typeof(From), typeof(IDictionary))) { FromT = SupIC.IDict; } else if (getInterface(typeof(From), typeof(IList))) { FromT = SupIC.IList; } else { return(default(To)); } } if (getInterface(typeof(To), typeof(ICollection))) { if (getInterface(typeof(To), typeof(Array))) { ToT = SupIC.IArr; } else if (getInterface(typeof(To), typeof(IDictionary))) { ToT = SupIC.IDict; } else if (getInterface(typeof(To), typeof(IList))) { ToT = SupIC.IList; } else { return(default(To)); } } if ((FromT == SupIC.Nullable) && (ToT == SupIC.Nullable)) { return(ConvertSimpleData <To, From>(obj)); } MethodInfo MI = System.Reflection.MethodBase.GetCurrentMethod() as MethodInfo; Type TO = typeof(To); Type FROM = typeof(From); Func <Array, List <int> > AGetRange = X => { List <int> Result = new List <int>(); for (int j = 0; j < X.Rank; j++) { Result.Add(X.GetLength(j)); } return(Result); }; Func <int, List <int>, List <int> > AGetNumbers = ((Num, Range) => { List <int> Result = new List <int>(); for (int j = 1; j < Range.Count; j++) { int Helper = 1; for (int k = j; k < Range.Count; k++) { Helper = Helper * Range[k]; } Result.Add(Num / Helper); Num = Num % Helper; } Result.Add(Num); return(Result); }); if (FromT == SupIC.IArr) { Array f = obj as Array; IList l = obj as IList; if (ToT == SupIC.IArr) { int N = typeof(To).Name.Count(x => x == ',') + 1; if (N == f.Rank) { List <int> ARange = AGetRange(f); Array r = (Array)Activator.CreateInstance(TO, AGetRange(f).Cast <object>().ToArray()); MI = MI.MakeGenericMethod(r.GetType().GetElementType(), f.GetType().GetElementType()); int j = 0; foreach (var e in l) { r.SetValue(MI.Invoke(this, new object[] { e, Delim }), AGetNumbers(j, ARange).ToArray()); j = j + 1; } return((To)(object)r); } else { return(default(To)); } } else if (ToT == SupIC.IDict) { IDictionary r = (IDictionary)NewInstance.GetInstance(GType(TO.FullName)); int j = 0; MethodInfo MI0 = MI.MakeGenericMethod(r.GetType().GetGenericArguments()[0], j.GetType()); MethodInfo MI1 = MI.MakeGenericMethod(r.GetType().GetGenericArguments()[1], f.GetType().GetElementType()); foreach (var e in l) { r.Add(MI0.Invoke(this, new object[] { j, Delim }), MI1.Invoke(this, new object[] { e, Delim })); j = j + 1; } return((To)(object)r); } else if (ToT == SupIC.IList) { IList r = (IList)NewInstance.GetInstance(GType(TO.FullName)); int j = 0; MI = MI.MakeGenericMethod(r.GetType().GetGenericArguments()[0], f.GetType().GetElementType()); foreach (var e in l) { r.Add(MI.Invoke(this, new object[] { e, Delim })); } return((To)(object)r); } else { string S = ""; MethodInfo MI0 = MI.MakeGenericMethod(typeof(string), f.GetType().GetElementType()); MI = MI.MakeGenericMethod(TO, typeof(string)); foreach (var e in l) { S = S + (string)MI0.Invoke(this, new object[] { e, Delim }) + Delim; } S = S.Substring(0, S.Length - Delim.Length); return((To)MI.Invoke(this, new object[] { S, Delim })); } } else if (FromT == SupIC.IDict) { IDictionary f = obj as IDictionary; if (ToT == SupIC.IArr) { int N = typeof(To).Name.Count(x => x == ',') + 1; List <int> ARange = new List <int>(N); for (int i = 0; i < N - 1; i++) { ARange.Add(1); } ARange.Add(f.Count); Array r = (Array)Activator.CreateInstance(TO, ARange.Cast <object>().ToArray()); MI = MI.MakeGenericMethod(r.GetType().GetElementType(), f.GetType().GetGenericArguments()[1]); int j = 0; foreach (var e in f) { object E = e.GetType().GetProperty("Value").GetValue(e, null); r.SetValue(MI.Invoke(this, new object[] { E, Delim }), AGetNumbers(j, ARange).ToArray()); j = j + 1; } return((To)(object)r); } else if (ToT == SupIC.IDict) { IDictionary r = (IDictionary)NewInstance.GetInstance(GType(TO.FullName)); MethodInfo MI0 = MI.MakeGenericMethod(r.GetType().GetGenericArguments()[0], f.GetType().GetGenericArguments()[0]); MethodInfo MI1 = MI.MakeGenericMethod(r.GetType().GetGenericArguments()[1], f.GetType().GetGenericArguments()[1]); foreach (var e in f) { object E0 = e.GetType().GetProperty("Key").GetValue(e, null); object E1 = e.GetType().GetProperty("Value").GetValue(e, null); r.Add(MI0.Invoke(this, new object[] { E0, Delim }), MI1.Invoke(this, new object[] { E1, Delim })); } return((To)(object)r); } else if (ToT == SupIC.IList) { IList r = (IList)NewInstance.GetInstance(GType(TO.FullName)); int j = 0; MI = MI.MakeGenericMethod(r.GetType().GetGenericArguments()[0], f.GetType().GetGenericArguments()[1]); foreach (var e in f) { object E = e.GetType().GetProperty("Value").GetValue(e, null); r.Add(MI.Invoke(this, new object[] { E, Delim })); j = j + 1; } return((To)(object)r); } else { string S = ""; MethodInfo MI0 = MI.MakeGenericMethod(typeof(string), f.GetType().GetGenericArguments()[1]); MI = MI.MakeGenericMethod(TO, typeof(string)); foreach (var e in f) { object E = e.GetType().GetProperty("Value").GetValue(e, null); S = S + (string)MI0.Invoke(this, new object[] { E, Delim }) + Delim; } S = S.Substring(0, S.Length - Delim.Length); return((To)MI.Invoke(this, new object[] { S, Delim })); } } else if (FromT == SupIC.IList) { IList l = obj as IList; if (ToT == SupIC.IArr) { int N = typeof(To).Name.Count(x => x == ',') + 1; List <int> ARange = new List <int>(N); for (int i = 0; i < N - 1; i++) { ARange.Add(1); } ARange.Add(l.Count); Array r = (Array)Activator.CreateInstance(TO, ARange.Cast <object>().ToArray()); MI = MI.MakeGenericMethod(r.GetType().GetElementType(), l.GetType().GetGenericArguments()[0]); int j = 0; foreach (var e in l) { r.SetValue(MI.Invoke(this, new object[] { e, Delim }), AGetNumbers(j, ARange).ToArray()); j = j + 1; } return((To)(object)r); } else if (ToT == SupIC.IDict) { IDictionary r = (IDictionary)NewInstance.GetInstance(GType(TO.FullName)); int j = 0; MethodInfo MI0 = MI.MakeGenericMethod(r.GetType().GetGenericArguments()[0], j.GetType()); MethodInfo MI1 = MI.MakeGenericMethod(r.GetType().GetGenericArguments()[1], l.GetType().GetGenericArguments()[0]); foreach (var e in l) { r.Add(MI0.Invoke(this, new object[] { j, Delim }), MI1.Invoke(this, new object[] { e, Delim })); j = j + 1; } return((To)(object)r); } else if (ToT == SupIC.IList) { IList r = (IList)NewInstance.GetInstance(GType(TO.FullName)); MI = MI.MakeGenericMethod(r.GetType().GetGenericArguments()[0], l.GetType().GetGenericArguments()[0]); foreach (var e in l) { r.Add(MI.Invoke(this, new object[] { e, Delim })); } return((To)(object)r); } else { string S = ""; MethodInfo MI0 = MI.MakeGenericMethod(typeof(string), l.GetType().GetGenericArguments()[0]); MI = MI.MakeGenericMethod(TO, typeof(string)); foreach (var e in l) { S = S + (string)MI0.Invoke(this, new object[] { e, Delim }) + Delim; } S = S.Substring(0, S.Length - Delim.Length); return((To)MI.Invoke(this, new object[] { S, Delim })); } } return(default(To)); }