class Employee
{
int Id { get; set; }
string Name { get; set; }
}
class ServiceIEmployeeModel
{
int Id { get; set; }
string Name { get; set; }
}
上記の従業員とServiceIEmployeeModel
クラスはそのビュー表現のために自動マップされるべきです。私はレコードのリストを返すようなC#でジェネリックメソッドを作成しようとしています
//My Generic Method below
public List<U> ListMapping<T, U>(List<T> sourceClass)
{
var config = new MapperConfiguration(cfg => { cfg.CreateMap<T, U>(); });
IMapper mapper = config.CreateMapper();
List<U> finalData = mapper.Map<List<T>, List<U>>(sourceClass);
return finalData;
}
List<Employee> employeeData = new List<Employee>();
List<ServiceIEmployeeModel> obj = new List<ServiceIEmployeeModel>();
ServiceIEmployeeModel obj = new ServiceIEmployeeModel();
obj.Id = 2;
obj.Name = "xyz";
employeeData =
ListMapping<List<ServiceIEmployeeModel>, List<Employee>>(sourceClass:obj);
私はレコードのリストを返すために、C#で汎用メソッドを作成しようとしています。
私は、ジェネリックメソッドを呼び出すには、次のコードを持っている:あなたのListMapping
メソッドはパラメータを入力受け入れ、その種類のリストをマップしようと
But i get an error saying :- Error CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List<ServiceIEmployeeModel>' to 'System.Collections.Generic.List<System.Collections.Generic.List<ServiceIEmployeeModel>>' Msc.MasterData.Presentation.Web C:\Users\sabarimani.a\Desktop\Presentation.Web\Web\Controllers\EmployeeController.cs 59 Active
私はちょうどそれはあなたがクラスをマッピングするたびにマッピングを作成することは非常に悪い考えだということに注意したい。代わりのように、このメソッドを呼び出します。一度だけ実行してから、マッパーにDIを注入してください。そうでない場合は、静的マッパーを使用してオブジェクトをマッピングします。 – juunas