如何重构这两种方法?第2部分. [英] How do I refactor these 2 methods? Part 2.

查看:35
本文介绍了如何重构这两种方法?第2部分.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的重构问题的第 2 部分.我在这里成功重构了我以前的方法:

This is the part 2 of my refactor problem. I have successfully refactor my previous method in here:

我如何重构这两种方法?

第二部分类似,但是我无法成功重构这两种方法.使用泛型方法时,我被卡在了行上:

The second part is similar, however I am unable to successfully refactor this 2 methods. When using a generic method, I was stuck at the line:

temp = (T)Convert.ToInt(value); 

所以不知何故我需要一种不同的方法来重构这两种方法.

So somehow I need a different approach to refactor these 2 methods.

public static void readDataDouble(string value, ref double[][] data, ref DateTime[] timeframe, ref DateTime[] date)
{

    string inputFile = "D:\\temp.csv";
    string[][] temp = null;

    if (File.Exists(inputFile))
    {
        string[] proRataVolumeFile = File.ReadAllLines(inputFile);
        temp = new string[proRataVolumeFile.Length][];

        for (int i = 0; i < proRataVolumeFile.Length; i++)
        {
            temp[i] = proRataVolumeFile[i].Split(',');
        }
    }
    date = new DateTime[temp.Length - 1];
    timeframe = new DateTime[temp[0].Length - 1];
    data = new double[temp.Length - 1][];

    for (int i = 1; i < temp.Length; i++)
    {
        data[i - 1] = new double[temp[i].Length - 1];

        for (int j = 1; j < temp[i].Length; j++)
        {
            if (temp[i][j].Length > 0)
                data[i - 1][j - 1] = Convert.ToDouble(temp[i][j]);
        }
    }

    for (int i = 1; i < temp.Length; i++)
    {
        date[i - 1] = Convert.ToDateTime(temp[i][0]);
    }

    for (int j = 1; j < temp[0].Length; j++)
    {
        timeframe[j - 1] = DateTime.Parse(temp[0][j]);
    }
}

public static void readDataInt(string value, ref int[][] data, ref DateTime[] timeframe, ref DateTime[] date)
{

    string inputFile = "D:\\temp.csv";
    string[][] temp = null;

    if (File.Exists(inputFile))
    {
        string[] proRataVolumeFile = File.ReadAllLines(inputFile);
        temp = new string[proRataVolumeFile.Length][];

        for (int i = 0; i < proRataVolumeFile.Length; i++)
        {
            temp[i] = proRataVolumeFile[i].Split(',');
        }
    }
           //convert the string to int

    date = new DateTime[temp.Length - 1];
    timeframe = new DateTime[temp[0].Length - 1];
    data = new int[temp.Length - 1][];

    for (int i = 1; i < temp.Length; i++)
    {
        data[i - 1] = new int[temp[i].Length - 1];

        for (int j = 1; j < temp[i].Length; j++)
        {
            if (temp[i][j].Length > 0)
                data[i - 1][j - 1] = Convert.ToInt32(temp[i][j]);
        }
    }

    for (int i = 1; i < temp.Length; i++)
    {
        date[i - 1] = DateTime.Parse(temp[i][0]);
    }

    for (int j = 1; j < temp[0].Length; j++)
    {
        timeframe[j - 1] = DateTime.Parse(temp[0][j]);
    }
}

如果有人发布一些针对此问题的工作片段以及我该如何称呼它,我将不胜感激.

I'd appreciate if someone post some working snippet to this problem and how can i call it.

谢谢.

推荐答案

你可以再次使用泛型:

    public static void readDataInt<T>(string value, ref T[][] data, ref DateTime[] timeframe, ref DateTime[] date)
    {
        string inputFile = "D:\\temp.csv";
        string[][] temp = null;

        if (File.Exists(inputFile))
        {
            string[] proRataVolumeFile = File.ReadAllLines(inputFile);
            temp = new string[proRataVolumeFile.Length][];

            for (int i = 0; i < proRataVolumeFile.Length; i++)
            {
                temp[i] = proRataVolumeFile[i].Split(',');
            }
        }

        date = new DateTime[temp.Length - 1];
        timeframe = new DateTime[temp[0].Length - 1];
        data = new T[temp.Length - 1][];

        for (int i = 1; i < temp.Length; i++)
        {
            data[i - 1] = new T[temp[i].Length - 1];

            for (int j = 1; j < temp[i].Length; j++)
            {
                if (temp[i][j].Length > 0)
                    data[i - 1][j - 1] = (T)((IConvertible)temp[i][j]).ToType(typeof(T),
                        System.Globalization.CultureInfo.InvariantCulture);
            }
        }

        for (int i = 1; i < temp.Length; i++)
        {
            date[i - 1] = DateTime.Parse(temp[i][0]);
        }

        for (int j = 1; j < temp[0].Length; j++)
        {
            timeframe[j - 1] = DateTime.Parse(temp[0][j]);
        }
    }

同样,您必须将强类型数组更改为其通用对应物,除此之外,唯一值得注意的更改是发生转换的行.由于 string 实现了 IConvertible,您可以使用 IConvertible.ToType() 方法将其转换为任何其他(支持的)类型.支持到原始类型的转换,对于不太重要的转换,您可以考虑使用像 通用类型转换器.请注意,现在转换需要一个 IFormatProvider(例如,对于不同的文化以不同的方式表示双打).

Again you have to change strongly typed array to its generic counterpart, besides this the only notable change is the line where the conversion occurs. Because string implements IConvertible you can use IConvertible.ToType() method to convert it to any other (supported) type. Conversions to primitive types are supported, for less trivial conversions you may consider to use a conversion library like Universal Type Converter. Please note that now the conversion needs an IFormatProvider (doubles, for example, are represented in different ways for different cultures).

这篇关于如何重构这两种方法?第2部分.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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