Skip to content

Instantly share code, notes, and snippets.

@farhad-taran
Created February 25, 2015 18:55
Show Gist options
  • Save farhad-taran/52f96e83bac35dad5d25 to your computer and use it in GitHub Desktop.
Save farhad-taran/52f96e83bac35dad5d25 to your computer and use it in GitHub Desktop.
mapper
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