Created
February 25, 2015 18:55
-
-
Save farhad-taran/52f96e83bac35dad5d25 to your computer and use it in GitHub Desktop.
mapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IMapper<out T> | |
{ | |
T Map(SqlMapper.GridReader reader); | |
} | |
public abstract class MapperBase<T> : IMapper<T> | |
{ | |
public abstract T Map(SqlMapper.GridReader reader); | |
public T map(dynamic item) | |
{ | |
var entity = Activator.CreateInstance<T>(); | |
//ExpandoObject implements dictionary | |
var properties = item as IDictionary<string, object>; | |
if (properties == null) | |
return entity; | |
foreach (var entry in properties) | |
{ | |
var propertyInfo = entity.GetType().GetProperty(entry.Key); | |
if (propertyInfo != null) | |
propertyInfo.SetValue(entity, entry.Value, null); | |
} | |
return entity; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment