Generic with multiple classes
First of all, your code that tries to set two type constraints on generic parameter T1
does not compile
where T1 : Supplier, new()
where T1 : Employee, new()
with the following error:
A constraint clause has already been specified for type parameter 'T1'. All of the constraints for a type parameter must be specified in a single where clause.
As MSDN article states you can use only one where
constraint on each generic parameter (see http://msdn.microsoft.com/en-us/library/bb384067.aspx).
"With multiple type parameters, use one where clause for each type parameter..."
You also cannot put multiple class names into one 'where' constraint. Only one class name and several interfaces.
where T1 : Supplier, IContractor, IComparable, new()
Keep in mind that this constraint dictates that the actual type you provide as the generic parameter T1
must be a successor of the Supplier
class or Supplier
class itself AND it has to implement both IContractor
AND IComparable
interfaces.
As soon as your method accepts a MyEntity
object and you do not specify what relation it has to Employee
and Supplier
classes, I cannot guess how this MyEntity
class knows about Employee
and Supplier
classes and how this relation helps you.
The only thing I can suggest is either to create an interface or a base class and inherit both of your classes from it. This is the only good reason I see for creating a generic method. It could look like this:
class Program
{
static void Main(string[] args)
{
Method1<Employee>();
Method1<Supplier>();
}
private static void Method1<T1>()
where T1 : IContractor, new()
{
}
}
public class Supplier : IContractor
{
string IContractor.Name
{
get{return "Supplier-Mufflier";}
}
}
public class Employee : IContractor
{
string IContractor.Name
{
get{return "Employee-Merloyee";}
}
}
public interface IContractor
{
string Name
{
get;
}
}
If your classes Supplier and Employee do not have something important in common that is enough for creating a common interface they could implement then you should not make a generic method for processing them.
Create an overloaded method for each of such types.
Imagine you have two classes: Wife
and Wine
. Both have an attribute of Age
and of the same type too. But do not even think of creating a common interface IAged
for those classes. The essence of the classes and the meaning of the Age
is so different that one should never unify them. Nevertheless some common logic might perfectly serve you. For example:
private double AgeQualify(Wife someWife)
{
return 1 / (someWife.Age * someWife.Beachness);
}
private double AgeQualify(Wine someWine)
{
return someWine.Age / someWine.Sugar;
}
You either need to make separate versions:
private string ConcatenateText<T1, T2>(MyEntity myEntity)
where T1 : Supplier, new()
where T2 : SupplierDepartment, new()
{
T1 p = new T1();
T2 r = new T2();
return mystring;
}
private string ConcatenateText<T1, T2>(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<T1, T2>(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)