I want to map my objects with generic extension methods.
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public Address HomeAddress { get; set; }
public string GetFullName()
{
return string.Format(“{0} {1}”, FirstName, LastName);
}
}
And this is viewmodel
public class CustomerListViewModel
{
public string FullName { get; set; }
public string Email { get; set; }
public string HomeAddressCountry { get; set; }
}
So I am creating map, Mapper.CreateMap<Customer, CustomerListViewModel>();
And I want to create an extension method
public static class MapperHelper
{
public static CustomerListViewModel ToViewModel(this Customer cust)
{
return AutoMapper.Mapper.Map<Customer, CustomerListViewModel>(cust);
}
}
But I want to make generic this helper:
public static class MapperHelper<TSource, TDest>
{
public static TDest ToViewModel(this TSource cust)
{
return AutoMapper.Mapper.Map<TSource, TDest>(cust);
}
}
Gives error: Extension method can only be declared in non-generic, non-nested static class
If I can not make generic, I should create helper class for all mapping. Is there any way to solution?