Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Reflection;
- namespace DnDShared.Models.DTOs
- {
- public class DTO<T> where T : DTO<T>, new()
- {
- public static T CreateFrom(object Source)
- {
- MethodInfo FactoryFactory = typeof(DTO<>).GetMethod("CreateFrom");
- T Output = new T();
- foreach(var Property in typeof(T).GetProperties())
- {
- PropertyInfo SourceType = Source.GetType().GetProperty(Property.Name);
- if (SourceType != null)
- {
- if (Property.PropertyType == SourceType.PropertyType)
- {
- Property.SetValue(Output, SourceType.GetValue(Source));
- }
- else if(Property.PropertyType.IsSubclassOf(typeof(DTO<>)))
- {
- Property.SetValue(Output, FactoryFactory.MakeGenericMethod(new[] { Property.PropertyType }).Invoke(null, new[] { SourceType.GetValue(Source) }));
- }
- }
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement