Generic with multiple classes

后端 未结 3 467
星月不相逢
星月不相逢 2020-12-30 20:53

I\'m trying to create this generic method to simplify things but I think I messed it up! Can you help with my problem?

This compiles:

private string          


        
3条回答
  •  自闭症患者
    2020-12-30 21:22

    You either need to make separate versions:

    private string ConcatenateText(MyEntity myEntity) 
        where T1 : Supplier, new()
        where T2 : SupplierDepartment, new()  
    {
        T1 p = new T1();
        T2 r = new T2();
        return mystring;
    }
    
    private string ConcatenateText(MyEntity myEntity) 
        where T1 : Employee, new()
        where T2 : EmployeeDepartment, new()
    {
        T1 p = new T1();
        T2 r = new T2();
        return mystring;
    }
    

    or need to make them share a base class:

    private string ConcatenateText(MyEntity myEntity) 
        where T1 : EmployeeSuplierBase, new()
        where T2 : EmployeeSupplierDeparmentBase, new()
    {
        T1 p = new T1();
        T2 r = new T2();
        return mystring;
    }
    

    I'd prefer the separate versions, really, because with them they can't call it with Supplier and EmployeeDeparment (or vice versa)

提交回复
热议问题