lundi 25 octobre 2021

How To map List of Object inside object using Reflection

I searched for a solution to this problem, but I did not find a solution. Please help me.

I used the reflection to generate custom dynamic AutoMapper ,because I used library AutoMapper in .net core m but the performance is vary bad.

1. ClassesViewModel    

    public class ClassesViewModel 
         {
            public int ClassID {get;set;}
            public string ClassNames {get;set;}
            public List<StudedntViewModel > Studednts {get;set;}
         } 

 
    
    public Class StudedntViewModel 
    {
        public int StudedntID {get;set;}
        public int StudentName{get;set;}
    }


 2. ClassesDto

public class ClassesDto 
         {
            public int ClassID {get;set;}
            public string ClassNames {get;set;}
            public List<StudedntDto > Studednts {get;set;}
         }  
        
        public Class StudedntDto 
        {
            public int StudedntID {get;set;}
            public int StudentName{get;set;}
        }

now I need to mapping from ClassesViewmodel to ClassesDto , this below code i used to handle this cases.

        public static void MatchAndMap<TSource, TDestination>(this TSource source, TDestination destination)
where TSource : class, new()
where TDestination : class, new()
        {
            try
            {
                // source = ClasseViewModel
                // destination = ClassesDto
                if (source != null && destination != null)
                {
                    // Get All proprity from source (ClasseViewModel)
                    List<PropertyInfo> sourceProperties = source.GetType().GetProperties().ToList<PropertyInfo>();
                    // Get All proprity from source (ClasseViewDto)
                    List<PropertyInfo> destinationProperties = destination.GetType().GetProperties().ToList<PropertyInfo>();

                    foreach (PropertyInfo sourceProperty in sourceProperties)
                    {
                        // get the selected proprity  from destinationProperties(ClasseViewDto) based sourceProperty selected.
                        // in this line i have problem , the object StudentDto in destination is null 
                        PropertyInfo destinationProperty = destinationProperties.Find(item => item.Name == sourceProperty.Name);
                        var getMethod = sourceProperty.GetGetMethod();
                        // check if proprity is Collection
                        if (sourceProperty.PropertyType.Namespace.Contains("Collections"))
                        {
                            // get All data from object StudentViewModel
                            var arrayObject = getMethod.Invoke(source, null);

                            foreach (object element in (IEnumerable)arrayObject)
                            {
                                // Get All proprity from Student source (StudentViewModel)
                                List<PropertyInfo> SubSourcePropertiesByElement = element.GetType().GetProperties().ToList<PropertyInfo>();

                                foreach (PropertyInfo arrayObjPinfo in SubSourcePropertiesByElement)
                                {
                                    // here the problem  how can  assign value from object destinationProperty if the proprity "destinationProperty" is null
                                    destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
                                }
                            }
                        }
                        // this block if sourceProperty is not Collection 
                        else
                        {
                            if (destinationProperty != null)
                            {
                                try
                                {
                                    destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
                                }
                                catch (Exception ex)
                                {

                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }

I hope the problem is clear to everyone, thank you in advance.





Aucun commentaire:

Enregistrer un commentaire