具有多个类的通用 [英] Generic with multiple classes

查看:20
本文介绍了具有多个类的通用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建这种通用方法来简化事情,但我认为我搞砸了!你能帮我解决问题吗?

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

编译:

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

虽然这不能编译:

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

消息:类型供应商不能用作泛型类型或方法 ConcatenateText(MyEntity myEntity) 中的类型参数 T1.没有从供应商到员工的隐式引用转换

这能做到吗?我究竟做错了什么?可以改进吗?

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

而 MyEntity 只是另一个类,以便在这个通用方法中处理它!它与类型 T 无关.它只是一个参数.但很明显,我不能这样做,使用 2 种这样的类型.我认为我可以分配一个或另一个,独立于我的初始化的 CLR 可以根据我的需要做出反应.我会接受分享更多有关它的信息的答案.

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.

推荐答案

首先,您的代码试图在泛型参数 T1 上设置两个类型约束,但无法编译

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()

出现以下错误:

已为类型参数T1"指定了约束子句.必须在单个 where 子句中指定类型参数的所有约束.

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.

正如 MSDN 文章所述,您只能对每个通用参数使用一个 where 约束(请参阅 http://msdn.microsoft.com/en-us/library/bb384067.aspx).

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).

对于多个类型参数,每个类型参数使用一个 where 子句..."

"With multiple type parameters, use one where clause for each type parameter..."

您也不能将多个类名放入一个where"约束中.只有一个类名和几个接口.

You also cannot put multiple class names into one 'where' constraint. Only one class name and several interfaces.

where T1 : Supplier, IContractor, IComparable, new()

请记住,此约束规定您作为泛型参数提供的实际类型 T1 必须是 Supplier 类或 Supplier 类本身并且它必须同时实现 IContractorIComparable 接口.

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.

只要您的方法接受 MyEntity 对象并且您没有指定它与 EmployeeSupplier 类的关系,我就不能猜猜这个 MyEntity 类是如何知道 EmployeeSupplier 类的,以及这种关系如何帮助你.

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.

假设您有两个类:WifeWine.两者都具有 Age 的属性并且也是相同的类型.但是甚至不要考虑为这些类创建一个公共接口IAged.类的本质和 Age 的含义是如此不同,以至于人们永远不应将它们统一起来.尽管如此,一些常见的逻辑可能会完美地为您服务.例如:

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

这篇关于具有多个类的通用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆