c# - 具有多个类的泛型

标签 c# generics types parameters

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

编译:

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。没有从 Supplier 到 Employee 的隐式引用转换

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

编辑:

而 MyEntity 只是另一个类,以便在这个通用方法中处理它!它与类型 T 无关。它只是一个参数。但很明显我不能那样做,使用这样的 2 种类型。我认为我可以分配一个或另一个,独立于我的初始化的 CLR 可以按照我的意愿使用react。我会接受分享更多相关信息的答案。

最佳答案

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

where T1 : Supplier, new()
where T1 : Employee, new()

错误如下:

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

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

您也不能将多个类名放入一个“where”约束中。只有一个类名和几个接口(interface)。

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

请记住,此约束规定您作为通用参数 T1 提供的实际类型必须是 Supplier 类或 Supplier 类本身,它必须同时实现 IContractorIComparable 接口(interface)。

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

我唯一可以建议的是创建一个接口(interface)或一个基类,然后从中继承这两个类。这是我看到的创建通用方法的唯一充分理由。它可能看起来像这样:

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

如果您的 Supplier 和 Employee 类没有一些重要的共同点足以创建它们可以实现的公共(public)接口(interface),那么您不应该创建一个通用方法来处理它们。

为每个这样的类型创建一个重载方法。

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

private double AgeQualify(Wife someWife)
{
    return 1 / (someWife.Age * someWife.Beachness);
}

private double AgeQualify(Wine someWine)
{
    return someWine.Age / someWine.Sugar;
}

关于c# - 具有多个类的泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17814312/

相关文章:

c# - .NET 中另一个项目使用的项目中引用的 DLL

c# - 为什么每次调用该方法时 Compile() 都不运行?

c - 我如何使用 16 位整数类型?

java - 我可以用 Class<?> 做什么?

java - 带泛型的数组

objective-c - 与@encode相反

haskell - 透明地实现特定形式的动态类型

c# - 系统.InvalidCastException : Failed to convert parameter value from a XElement to a String

c# - 逐行读取word文档

c# - 如何通过 JObject 进行枚举?