I have two objects(classes) with exact properties. say :
class class1
{
public string prop1{get;set;}
public string prop2{get;set;}
}
class class2
{
public string prop1{get;set;}
public string prop2{get;set;}
}
I want to map these classes using reflection, I have already used AutoMapper and it doesn’t work for my situation, as I have object inside object….
When using reflection I need to pass the property name and i don’t want to do this one by one is there other way:
PropertyInfo propinfo = listToReturn.GetType().GetProperty(nameofproperty);
EDIT::
Here is what i have tried with automapper:
internal static DTO_objectclass ConvertFOS(objectclass q)
{
DTO_objectclass resultsToReturn = new DTO_objectclass();
AutoMapper.Mapper.CreateMap<objectclass , DTO_objectclass >();
resultsToReturn = AutoMapper.Mapper.Map<objectclass , DTO_objectclass>(q);
return resultsToReturn;
}
this works until it comes to a property where I have something like this in objectclass :
property class3 parentClass{get; set;}
and in DTO_objectclass i got :
property guid parentClass{get; set;}
where i get exception of failed to convert..
Trying to map System.Guid to parentclient.\nUsing mapping configuration for DTO_objectclass to objectclass \nDestination property: ParentClass\nException of type 'AutoMapper.AutoMapperMappingException' was thrown.
The exception explains the issue. AutoMapper can’t convert a GUID to a ParentClass. AutoMapper will assume that two properties/fields with the same name will be of the same type in the absence of better information.
To overcome this, you’ll need to write a conversion method. If that method were called
ConvertGuidToParentClassInstance, for example, you could then write a mapping like this:With such a mapping in place, AutoMapper can handle this case.