Calling a generic method with the correct derived type

后端 未结 3 1030
旧时难觅i
旧时难觅i 2021-02-08 20:28

I have the following scenario:

I have three classes, let\'s call them A, B and C. All they have in common is that they inherit fro

3条回答
  •  轮回少年
    2021-02-08 20:49

    You can apply dynamic keyword when passing entity to ProcessEntity. In this case actual type of entity will be determined at runtime.

    public void MyMethod(List entityList)
    {
      foreach(var entity in entityList)
      {
        dynamic obj = entity;
        ProcessEntity(obj);
      }
    }
    

提交回复
热议问题