public static object ToObject(string str, Type type) { if (type.IsGenericType) { if (type.GetGenericTypeDefinition() == typeof(List <>)) { Type[] types = type.GetGenericArguments(); Type t = types[0]; List <string> list = ToListObject(str); var result = type.GetConstructor(Type.EmptyTypes).Invoke(null); foreach (var item in list) { var v = ToObject(item, t); ListAddMethodInfo.Invoke(result, new object[] { v }); } return(result); } else if (type.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { Type[] types = type.GetGenericArguments(); Dictionary <string, string> map = ToMapObject(str); var result = type.GetConstructor(Type.EmptyTypes).Invoke(null); foreach (var item in map) { var key = ToObject(item.Key, types[0]); var v = ToObject(item.Value, types[1]); DictionaryAddMethodInfo.Invoke(result, new object[] { key, v }); } return(result); } DebugUtils.Assert(false, "ToObject Not Found Type: " + type.FullName); return(null); } else if (type.BaseType == typeof(Enum)) { return(EnumUtils.StringToEnum(str, type)); } MethodInfo method = GetToObjectMethodInfoFromCache(type); GenericParametersObjectTwo[0] = str; GenericParametersObjectTwo[1] = GetDefault(type); bool res = (bool)method.Invoke(null, GenericParametersObjectTwo); DebugUtils.Assert(res, string.Format("Data {0} Not Right Type {1}", str, type.FullName)); return(GenericParametersObjectTwo[1]); }
internal static DictionaryAddMethodInfo GetIDictionaryAddMethod(Type dictionaryType, Type keyType, Type valueType) { MethodInfo methodInfo; try { methodInfo = dictionaryType.GetMethod(nameof(IDictionary.Add), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, Type.DefaultBinder, new[] { keyType, valueType }, null); if (methodInfo != null) { var args = methodInfo.GetParameters(); var firstArg = args[0]; var secondArg = args[1]; var result = new DictionaryAddMethodInfo { MethodInfo = methodInfo, HasRetunValue = methodInfo.ReturnType != typeof(void) }; if (firstArg.ParameterType != keyType) { if (firstArg.ParameterType == typeof(object)) { result.NeedsArgumentBoxing = true; } else { result = null; } } if (result != null && secondArg.ParameterType != valueType) { if (secondArg.ParameterType == typeof(object)) { result.ValueNeedsArgumentBoxing = true; } else { result = null; } } if (result != null) { return(result); } } } catch (Exception) { } try { methodInfo = dictionaryType.GetMethod(nameof(IDictionary.Add), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, Type.DefaultBinder, new[] { typeof(object), typeof(object) }, null); if (methodInfo != null) { return(new DictionaryAddMethodInfo() { MethodInfo = methodInfo, HasRetunValue = methodInfo.ReturnType != typeof(void), NeedsArgumentBoxing = true, ValueNeedsArgumentBoxing = true }); } } catch (Exception) { } try { if (CompareInterfaceGenericTypeDefinition(dictionaryType.GetInterfaces(), typeof(IDictionary <,>))) { var genericInterfaceType = typeof(IDictionary <,>).MakeGenericType(keyType, valueType); methodInfo = genericInterfaceType.GetMethod(nameof(IDictionary.Add)); if (methodInfo != null) { return(new DictionaryAddMethodInfo() { MethodInfo = methodInfo, HasRetunValue = methodInfo.ReturnType != typeof(void), NeedsArgumentBoxing = true, ValueNeedsArgumentBoxing = true, NeedsIDictionaryBoxing = true, BoxingIDictionaryType = genericInterfaceType }); } } } catch (Exception) { } throw new InvalidTypeException($"Dictionary type '{dictionaryType}' does not have valid Add method."); }