How to automatically map the values between instances of two different classes if both have same properties?

后端 未结 5 1832
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 11:10

I have two classes with exactly same members (properties and fields) with same datatypes. I want to map the members from one to other in automated way. I know there are more

5条回答
  •  醉话见心
    2020-12-30 11:47

    Extending from accepted answer from @pasty, I have created generic method for this purpose.

    public static TDest MapSourceToDest(TSource source)
                                        where TSource : class//We are not creating an instance of source, no need to restrict parameterless constructor
                                        where TDest : class, new()//We are creating an instance of destination, parameterless constructor is needed
    {
        if(source == null)
            return null;
    
        TDest destination = new TDest();
    
        var typeOfSource = source.GetType();
        var typeOfDestination = destination.GetType();
    
        foreach(var fieldOfSource in typeOfSource.GetFields())
        {
            var fieldOfDestination = typeOfDestination.GetField(fieldOfSource.Name);
            if(fieldOfDestination != null)
            {
                try
                { fieldOfDestination.SetValue(destination, fieldOfSource.GetValue(source)); }
                catch(ArgumentException) { }//If datatype is mismatch, skip the mapping
            }
        }
    
        foreach(var propertyOfSource in typeOfSource.GetProperties())
        {
            var propertyOfDestination = typeOfDestination.GetProperty(propertyOfSource.Name);
            if(propertyOfDestination != null)
            {
                try
                { propertyOfDestination.SetValue(destination, propertyOfSource.GetValue(source)); }
                catch(ArgumentException) { }//If datatype is mismatch, skip the mapping
            }
        }
    
        return destination;
    }
    

    One may need to alter the filters on generic types; but everything else will work cross any types. Null check is added for fieldOfDestination and propertyOfDestination just in case a member is missing; this adds up little more flexibility.

提交回复
热议问题