使方法通用

时间:2016-06-30 14:37:21

标签: c# generics

我有一个从特定对象中提取天名的方法, 像这样:

private string ExtractWeekDayNames(FiscalReceipt fiscalReceipt)
    {
        string retVal = "";

        Dictionary<string, bool> WeekDays = 
            new Dictionary<string, bool>() {
                                            { "Sun", fiscalReceipt.Sunday },
                                            { "Mon", fiscalReceipt.Monday },
                                            { "Tue", fiscalReceipt.Tuesday },
                                            { "Wed", fiscalReceipt.Wednesday },
                                            { "Thu", fiscalReceipt.Thursday },
                                            { "Fri", fiscalReceipt.Friday },
                                            { "Sat", fiscalReceipt.Saturday }
                                            };


        //Find week days
        foreach (var item in WeekDays)
        {
            if (item.Value == true)
                retVal += item.Key + ",";
        }

        if (!string.IsNullOrEmpty(retVal))
            retVal = retVal.Substring(0, retVal.Length - 1);

        return retVal;

    }

我也有类似的方法执行相同的操作 但有一个不同类型的参数,如:

private string ExtractWeekDayNames(NonFiscalReceipt nonFiscalReceipt)
{
    ...
}

NonFiscalReceipt还有周日,周一等属性。

如何用一个方法替换这两个方法?

3 个答案:

答案 0 :(得分:2)

public class FiscalReceipt : Receipt{
FiscalReceipt specific fields
}

public class NonFiscalReceipt : Receipt{
NonFiscalReceipt specific fields..
}

public class Receipt{
fields common to both classes
}

private string ExtractWeekDayNames(Receipt receipt){

}

两种类型的收据都是从收据继承的,即您可以传入,并且仍然会包含所有字段:)

答案 1 :(得分:1)

您需要为要实现的类创建一个公共接口,以便该方法可以接受实现此接口的任何内容。

interface IReceipt
{
    bool Sunday {get; }
    bool Monday {get; }
    ...
    bool Saturday {get; }
}

然后你的方法应该是这样的:

private string ExtractWeekDayNames<T>(T receipt) where T : IReceipt
{
    string retVal = "";

    Dictionary<string, bool> WeekDays = 
        new Dictionary<string, bool>() {
                                        { "Sun", receipt.Sunday },
                                        { "Mon", fiscalReceipt.Monday },
                                        { "Tue", receipt.Tuesday },
                                        { "Wed", receipt.Wednesday },
                                        { "Thu", receipt.Thursday },
                                        { "Fri", receipt.Friday },
                                        { "Sat", receipt.Saturday }
                                        };


    //Find week days
    foreach (var item in WeekDays)
    {
        if (item.Value == true)
            retVal += item.Key + ",";
    }

    if (!string.IsNullOrEmpty(retVal))
        retVal = retVal.Substring(0, retVal.Length - 1);

    return retVal;

}

然而,正如juharr和Remy Grandin在评论中写的那样 - 在这种情况下不需要泛型 - 你可以简单地将界面传递给方法:

private string ExtractWeekDayNames(IReceipt receipt) 
{
    string retVal = "";

    Dictionary<string, bool> WeekDays = 
        new Dictionary<string, bool>() {
                                        { "Sun", receipt.Sunday },
                                        { "Mon", receipt.Monday },
                                        { "Tue", receipt.Tuesday },
                                        { "Wed", receipt.Wednesday },
                                        { "Thu", receipt.Thursday },
                                        { "Fri", receipt.Friday },
                                        { "Sat", receipt.Saturday }
                                        };


    //Find week days
    foreach (var item in WeekDays)
    {
        if (item.Value == true)
            retVal += item.Key + ",";
    }

    if (!string.IsNullOrEmpty(retVal))
        retVal = retVal.Substring(0, retVal.Length - 1);

    return retVal;

}

答案 2 :(得分:-1)

为包含公共属性的NonFiscalReceiptFiscalReceipt创建基本界面:

public interface IReceipt
{
   bool Sunday{get;}
   ......
}

将方法签名替换为:private string ExtractWeekDayNames(IReceipt fiscalReceipt)

尽可能尝试使用DayOfWeek枚举。