I am writing a generic mapper that will map properties with the same type and name among two different object. To do so i used FastMember as in the code below :
public static void Map ( this object target, object source, bool ignoreCase = false )
{
if (source == null)
throw new ArgumentNullException("source");
if (target == null)
throw new ArgumentNullException("target");
var sourceType = source.GetType();
var targetType = target.GetType();
var sourceAcc = TypeAccessor.Create(sourceType);
var targetAcc = TypeAccessor.Create(targetType);
var sourceMembers = sourceAcc.GetMembers();
var targetMembers = targetAcc.GetMembers();
foreach ( var targetMember in targetMembers )
{
if (targetMember.CanWrite == false)
continue;
var sourceMember = sourceMembers.FirstOrDefault ( member => ((ignoreCase) ? member.Name.ToUpper() == targetMember.Name.ToUpper() : member.Name == targetMember.Name) &&
member.Type == targetMember.Type );
targetAcc[target, targetMember.Name] = sourceAcc[source, sourceMember.Name];
}
}
The problem is that when i write var sourceAcc = TypeAccessor.Create(sourceType);
i get an instance of DynamicAccessor that does not support GetMembers method. So then when i call GetMembers method i get an exception (NotSupportedException).
I would like to know which is the proper way to enumarte public properties of an instance (given it's type)
Aucun commentaire:
Enregistrer un commentaire