Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

c# - Generic with multiple classes

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 ConcatenateText<T1, T2>(MyEntity myEntity) 
    where T1 : Supplier, new()
    where T1 : Employee, new()
    where T2 : SupplierDepartment, new()
    where T2 : EmployeeDepartment, new()
{
    T1 p = new T1();
    T2 r = new T2();
    //Code here for myEntity treatment
    return mystring;
}

While this does not compile:

protected void mybutton1_Click(object sender, EventArgs e)
{
   string mystring = ConcatenaText<Supplier, SupplierDepartment>(myEntity);
}

//This does not compile
protected void mybutton2_Click(object sender, EventArgs e)
{
   string mystring = ConcatenaText<Employee, EmployeeDepartment>(myEntity);
}

Message: The type Supplier cannot be used as type parameter T1 in the generic type or method ConcatenateText(MyEntity myEntity). There is no implicit reference conversion from Supplier to Employee

Can this be done? What am I doing wrong? Can it be improved?

EDIT:

And MyEntity is just another class in order to process it inside this generic method! It's not related to the types T. It just an argument. But it's clear that I can't do that, using 2 types like that. I thought that I could assign one or another and the CLR independently of my initialization could react as I wanted. I gonna accept the answer who share a little bit more information about it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...