jeudi 27 juillet 2017

Create a generic function that return a generic type list in C#

I have two Model classes, one is Person and the other is Department. What I want to achieve is I want to create a generic function which can be used to retrieve generic type data in the form of List for e.g. one call will return Person List , another call will return Department List. More specifically I want to return data in the form of a List.

The below is the Person Class.

public class Person {

    public Int16 Personid{ get; set; }
    public string Personname { get; set; }
    public string Personaddress { get; set; }

}

The below is the Department class.

public class Department {

     public Int16 Departmentid { get; set; }
     public string Departmentname { get; set; }
     public string Departmentsection { get; set; }

}

The below are the calling function where One time I make a call with CallingMethodPerson() and other with CallingMethodDepartment().

public class CallingClass {

   public void CallingMethodPerson() {

      CalledClass calling = new CalledClass();
      Calling. CalledMethod();

   }

    public void CallingMethodDepartment() {

      CalledClass calling = new CalledClass();
      Calling. CalledMethod();

    }
}

The below blue print is the CalledClass where it does some manipulation and return List of either Person or Department.

public class CalledClass {

    public void CalledMethod() {
      //this is a generic method wherein returns the list of either a Person 
      or Department whenever called.

    }
}

To summarise I want to implement a common function that return generic type data.





Aucun commentaire:

Enregistrer un commentaire