简体   繁体   English

有人可以帮助我理解此示例的泛型吗?

[英]Can someone helps me on understanding generics for this example?

public class KI_Payments<TInfo1 ,TInfo2 , TInfo3, TInfo4, TInfo5> : KI_Leaf
{
    TInfo1 A....
    TInfo2 B.....
}

KI_Payments<TInfo1,TInfo2,TInfo3,TInfo4,TInfo5> oPayment = new KI_Payments<TInfo1,TInfo2,TInfo3,TInfo4,TInfo5>();
switch (Something)
{
     case 1:
        oPayment = new KI_Payments<string, string, string,string,string>();
     case 2:
       oPayment = new KI_Payments<string, int, int,int,int>();

}

What I am trying to do is depending on the "Something", i want to declare info1-info5 with different declarations. 我想做的是取决于“东西”,我想用不同的声明来声明info1-info5。
Any thoughts? 有什么想法吗? Thanks 谢谢

Note that generic payments with different type parameters will not be assignment compatible. 请注意,具有不同类型参数的通用付款将不兼容分配。 You cannot do this 你不可以做这个

KI_Payment<T1,T2,T3,T4,T5> p = 
    new KI_Payment<string,int,int,int,int>(); // Does not compile!

KI_Payment<object,object,object,object,object> p = 
    new KI_Payment<string,int,int,int,int>(); // Does not compile!

Instead, create a base class having only string properties that you can load from and save to the DB. 而是,创建仅具有字符串属性的基类,您可以从中加载并保存到数据库。

public abstract class PaymentBase
{
    public string Info1 { get; set; }
    public string Info2 { get; set; }
    public string Info3 { get; set; }
    public string Info4 { get; set; }
    public string Info5 { get; set; }
}

Then create specialized payment types with specialized properties, which are just wrappers for the string properties. 然后创建具有特殊属性的特殊付款类型,这些属性只是字符串属性的包装。 These properties convert from and to string if necessary. 必要时,这些属性可从字符串转换为字符串。

public class PaymentTypeX : PaymentBase
{
    public string Name
    {
        get { return Info1; }
        set { Info1 = value; }
    }

    public int Number
    {
        get {
            int n;
            Int32.TryParse(Info2, out n);
            return n; 
        }
        set { Info2 = value.ToString(); }
    }
}

Finally instantiate specific payment types 最后实例化特定的付款类型

PaymentBase payment;
switch (Something) { 
    case 1:         
        payment = new PaymentTypeX();
        break;
    case 2:
        payment = new PaymentTypeY();
        break;
} 

Say info1 is a credit card number in one instantiation and a check number in another, are you actually going to treat them exactly the same in the KI_Payments code? 假设info1在一个实例中是一个信用卡号,而在另一个实例中是一个支票号,您实际上将在KI_Payments代码中对待它们吗?

It might be better if you make a class hierarchy with the payment types you need. 如果使用所需的付款类型建立类层次结构,可能会更好。 This way you can better do code reuse if you do need to do special handling of one of these TInfos . 这样,如果您确实需要对这些TInfos之一进行特殊处理,则可以更好地进行代码重用。 Since your table has string columns, this would make it easier for your constructors to take 5 string parameters and interpret them properly. 由于您的表具有字符串列,因此使构造函数更容易采用5个字符串参数并正确解释它们。 You have not specified what the Something is but you could use something like the Factory pattern to implement the object creation. 您尚未指定Something是什么,但是可以使用Factory模式之类的东西来实现对象创建。

On a related topic, having tables with string columns where you actually store other types of data is arguably not a good idea especially if we are talking about databases. 在一个相关的主题上,拥有一个带有字符串列的表来实际存储其他类型的数据可能不是一个好主意,特别是在我们谈论数据库的时候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM