通用多类 [英] Generic with multiple classes

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

问题描述

您好球员,我试图创建这个通用的方法,简化的事情,但我想我搞砸了! ?你可以用我的问题,帮助



这编译:

 私人串ConcatenateText< T1,T2>(myEntity所myEntity所)
,其中T1:供应商,新的()
,其中T1:员工,新的()
,其中T2:SupplierDepartment,新的()
,其中T2:EmployeeDepartment,新的()
{
T1 p = T1新();
T2 R =新T2();
//代码这里myEntity所治疗
返回了mystring;
}



虽然这并不编译:

 保护无效mybutton1_Click(对象发件人,EventArgs五)
{
串了mystring = ConcatenaText<供应商,SupplierDepartment>(myEntity所);
}

//这不能编译
保护无效mybutton2_Click(对象发件人,EventArgs五)
{
串了mystring = ConcatenaText<员工,EmployeeDepartment> (myEntity所);
}



消息:该类型的供应商不能用作类型参数T1在泛型类型或方法ConcatenateText(myEntity所myEntity所)。有一个从供应商到员工没有隐式引用转换



可以这样做?我究竟做错了什么?它可以改善?




----- -----编辑




感谢您回答我的问题。而myEntity所仅仅是另一个类,以proccess它这个泛型方法里面!它不相关的各类T.它只是一个参数。但很显然,我不能做到这一点,使用2种这样的。我以为我可以指定一个或另一个和CLR独立的,因为我想我的初始化才反应过来。我该怎么接受谁分享它一点点的更多信息了答案。谢谢你。


解决方案

首先,你的代码,试图在泛型参数T1设置两种类型的限制不会编译




 其中T1:供应商,新的()
,其中T1:员工,新的()




,出现以下错误:




一个约束条款已经被指定类型参数T1。所有类型参数的约束必须在单个where子句来指定。




由于MSDN文章指出,你可以只用一个'其中,每个泛型参数约束(见 http://msdn.microsoft.com/ EN-US /库/ bb384067.aspx )。




如果有多个类型参数,使用一个where子句为每种类型参数......




您也不能把多个类名到一个',其中'约束。只有一个类名和几个接口

 其中T1:供应商,IContractor,IComparable的,新的()

记住,这个约束决定了您提供的泛型参数的实际类型T1必须是供应商类的继任者或供应商类本身,它必须实现两个IContractor和IComparable的接口。



只要你的方法接受一个myEntity所对象,并没有指定它有员工什么关系供应商类,我无法猜测这个myEntity所类是如何知道员工和供应商类,以及这种关系可以帮助你。



我可以建议的唯一一件事要么是创建一个接口或基类,并从它同时继承你的类。这是唯一的理由我看到了创建一个通用的方法。它可能是这样的:

 类节目
{
静态无效的主要(字串[] args)
{
:方法一LT;员工>();
:方法一LT;供应商>();
}

私有静态无效方法1< T1>()
,其中T1:IContractor,新的()
{

}
}

公共类供应商:IContractor
{
串IContractor.Name
{
{返回员工姓名;}
}
}

公共类员工:IContractor
{
串IContractor.Name
{
{返回用户名; }
}
}

公共接口IContractor
{
字符串名称
{
搞定;
}
}

如果你的类供应商和员工没有重要的事情在常见的是足以建立一个共同的接口,他们可以实现,那么你不应该做一个通用的方法来处理它们。



创建每个这种类型的重载的方法



想象一下,你有两个类:妻子葡萄酒。两者都有的年龄和同一类型过于的属性。但是,想都不要想创建一个通用的接口 IAged 这些类的。阶级的本质和年龄的含义是如此不同,一个人永远不能统一他们。不过一些常见的逻辑可能完全为您服务。例如:

 私人双AgeQualify(妻子someWife)
{
返回1 /(someWife.Age * someWife.Beachness);
}

私人双AgeQualify(酒someWine)
{
返回someWine.Age / someWine.Sugar;
}


Hello guys 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-----

Thanks for answer to my question. And MyEntity is just another class in order to proccess 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. Thanks.

解决方案

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 "Employee name";}
    }
}

public class Employee : IContractor
{
    string IContractor.Name
    {
        get{return "Customer name";}
    }
}

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

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

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