Generic with multiple classes

后端 未结 3 468
星月不相逢
星月不相逢 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:10

    You really should not use generics in that case. There are only two options.

    So:

    string ConcatenateText(Supplier Entity) { ...code...} 
    string ConcatenateText(Employee Entity) { ...code...}  
    

    What you can do is unify both with a base class concentrating all common methods.

    Say, if both supplier and employee has Name:

    class BaseClass
    {
        public string Name {get; set;}
    }
    
    class Employee : BaseClass
    {
        //emplyee stuff except name and other things already in base
    }
    
    class Supplier : BaseClass
    {
        //supplier stuff except name and other things already in base
    }
    

    And then, the method takes BaseClass:

    private string ConcatenateText(BaseClass Entity) 
    { 
        //code
    }
    

提交回复
热议问题