private static object ToObjectTest <T>(Document document, string className = "", string classNamespace = "") { var thisNamespace = !string.IsNullOrEmpty(classNamespace) ? classNamespace : typeof(T).Namespace; var classObject = Activator.CreateInstance(typeof(T)); var classPropertyInfo = classObject.GetType().GetProperties().FirstOrDefault(x => x.Name == className); if (classPropertyInfo != null) { var propertyType = classPropertyInfo.PropertyType; classObject = Activator.CreateInstance(propertyType); } if (document != null) { foreach (var entry in document) { var propertyInfo = TestObjects(entry.Key, classObject.GetType()); if (propertyInfo != null) { if (propertyInfo.PropertyType == typeof(string)) { propertyInfo.SetValue(classObject, entry.Value.AsString()); } else if (propertyInfo.PropertyType == typeof(int) || propertyInfo.PropertyType == typeof(int?)) { propertyInfo.SetValue(classObject, entry.Value.AsInt()); } else if (propertyInfo.PropertyType == typeof(bool) || propertyInfo.PropertyType == typeof(bool?)) { propertyInfo.SetValue(classObject, entry.Value.AsBoolean()); } else if (propertyInfo.PropertyType == typeof(List <string>)) { propertyInfo.SetValue(classObject, entry.Value.AsListOfString()); } else if (propertyInfo.PropertyType == typeof(List <int>)) { List <int> integerList = new List <int>(); foreach (var dynamoDbEntry in entry.Value.AsListOfDynamoDBEntry()) { integerList.Add(dynamoDbEntry.AsInt()); } propertyInfo.SetValue(classObject, integerList); } else if (propertyInfo.PropertyType == typeof(float) || propertyInfo.PropertyType == typeof(float?)) { propertyInfo.SetValue(classObject, (float)entry.Value.AsDouble()); // Needs testing. } else if (propertyInfo.PropertyType == typeof(double) || propertyInfo.PropertyType == typeof(double?)) { propertyInfo.SetValue(classObject, entry.Value.AsDouble()); // Needs testing. } else if (propertyInfo.PropertyType == typeof(uint)) { propertyInfo.SetValue(classObject, entry.Value.AsUInt()); // Needs testing. } else if (propertyInfo.PropertyType == typeof(long) || propertyInfo.PropertyType == typeof(long?)) { propertyInfo.SetValue(classObject, entry.Value.AsLong()); // Needs testing. } else if (propertyInfo.PropertyType == typeof(ulong) || propertyInfo.PropertyType == typeof(ulong?)) { propertyInfo.SetValue(classObject, entry.Value.AsULong()); // Needs testing. } else if (propertyInfo.PropertyType == typeof(char) || propertyInfo.PropertyType == typeof(char?)) { propertyInfo.SetValue(classObject, entry.Value.AsChar()); // Needs testing. } else if (propertyInfo.PropertyType == typeof(DateTime) || propertyInfo.PropertyType == typeof(DateTime?)) { propertyInfo.SetValue(classObject, entry.Value.AsDateTime()); // Needs testing. } else { Type valueType = entry.Value.GetType(); if (valueType == typeof(DynamoDBList)) { var dynamoDbList = entry.Value as DynamoDBList; var subType = propertyInfo.PropertyType.GetGenericArguments()[0]; var subClassName = subType.Name; var genericList = typeof(List <>); var stringList = genericList.MakeGenericType(subType); var list = Activator.CreateInstance(stringList); var addMethod = stringList.GetMethod("Add"); foreach (var dbEntry in dynamoDbList.Entries) { var tmpDoc = dbEntry as Document; var subObject = AWSDocumentConverter.ToObjectTest <T>(tmpDoc, subClassName); addMethod.Invoke(list, new object[] { subObject }); } propertyInfo.SetValue(classObject, list); } else if (valueType == typeof(Document)) { var tmpDoc = entry.Value as Document; var subDocuement = AWSDocumentConverter.ToObjectTest <T>(tmpDoc, propertyInfo.PropertyType.Name, propertyInfo.PropertyType.Namespace); propertyInfo.SetValue(classObject, subDocuement); } } } } } return(classObject); }
private static object ToObjectTest <T>(Document document, string className = "", string classNamespace = "") { var thisType = !string.IsNullOrEmpty(className) ? className : typeof(T).Name; var thisNamespace = !string.IsNullOrEmpty(classNamespace) ? classNamespace : typeof(T).Namespace; Type t = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()) .Where(qt => qt.Name == className) .FirstOrDefault(); var options = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()) .Where(qt => qt.Name == className).ToList(); var classObject2 = t != null ? t : Activator.CreateInstance(typeof(T)); var classObject = Activator.CreateInstance(typeof(T)); var classPropertyInfo = classObject.GetType().GetProperties().FirstOrDefault(x => x.Name == className); // If className is a property. Use this class instead. // Might be a problem if Person class doesn't contain for instance Rank class, but the Job class does. // Might need to deliver the subclass to next method call. if (classPropertyInfo != null) { var propertyType = classPropertyInfo.PropertyType; classObject = Activator.CreateInstance(propertyType); } string namespacetest = $"{thisNamespace}.{thisType}"; if (document != null) { foreach (var entry in document) { var propertyInfo = TestObjects(entry.Key, classObject.GetType()); if (propertyInfo != null) { if (propertyInfo.PropertyType == typeof(string)) { propertyInfo.SetValue(classObject, entry.Value.AsString()); } else if (propertyInfo.PropertyType == typeof(int) || propertyInfo.PropertyType == typeof(int?)) { propertyInfo.SetValue(classObject, entry.Value.AsInt()); } else if (propertyInfo.PropertyType == typeof(bool) || propertyInfo.PropertyType == typeof(bool?)) { propertyInfo.SetValue(classObject, entry.Value.AsBoolean()); } else if (propertyInfo.PropertyType == typeof(List <string>)) { propertyInfo.SetValue(classObject, entry.Value.AsListOfString()); } else if (propertyInfo.PropertyType == typeof(List <int>)) { List <int> integerList = new List <int>(); foreach (var dynamoDbEntry in entry.Value.AsListOfDynamoDBEntry()) { integerList.Add(dynamoDbEntry.AsInt()); } propertyInfo.SetValue(classObject, integerList); } else if (propertyInfo.PropertyType == typeof(float)) { propertyInfo.SetValue(classObject, (float)entry.Value.AsDouble()); // Needs testing. } else if (propertyInfo.PropertyType == typeof(double)) { propertyInfo.SetValue(classObject, entry.Value.AsDouble()); // Needs testing. } else if (propertyInfo.PropertyType == typeof(uint)) { propertyInfo.SetValue(classObject, entry.Value.AsUInt()); // Needs testing. } else if (propertyInfo.PropertyType == typeof(long)) { propertyInfo.SetValue(classObject, entry.Value.AsLong()); // Needs testing. } else if (propertyInfo.PropertyType == typeof(ulong)) { propertyInfo.SetValue(classObject, entry.Value.AsULong()); // Needs testing. } else if (propertyInfo.PropertyType == typeof(char)) { propertyInfo.SetValue(classObject, entry.Value.AsChar()); // Needs testing. } else { Type valueType = entry.Value.GetType(); if (valueType == typeof(DynamoDBList)) { var dynamoDbList = entry.Value as DynamoDBList; var subType = propertyInfo.PropertyType.GetGenericArguments()[0]; var subClassName = subType.Name; // Reflection way of working with list // var genericList = typeof(List <>); var stringList = genericList.MakeGenericType(subType); var list = Activator.CreateInstance(stringList); var addMethod = stringList.GetMethod("Add"); foreach (var dbEntry in dynamoDbList.Entries) { var tmpDoc = dbEntry as Document; var subObject = AWSDocumentConverter.ToObjectTest <T>(tmpDoc, subClassName); addMethod.Invoke(list, new object[] { subObject }); } propertyInfo.SetValue(classObject, list); } else if (valueType == typeof(Document)) { var tmpDoc = entry.Value as Document; var subDocuement = AWSDocumentConverter.ToObjectTest <T>(tmpDoc, propertyInfo.PropertyType.Name, propertyInfo.PropertyType.Namespace); propertyInfo.SetValue(classObject, subDocuement); } } } } } return(classObject); }