从文本文件

时间:2015-10-05 22:11:03

标签: c# generics text io

我使用以下方法创建通用集合数据的保存文件:

public static void WriteDataListToFile<T>(T dataList, string folderPath, string fileName) where T : IEnumerable, ICollection
{
    //Check to see if file already exists
    if(!File.Exists(folderPath + fileName))
    {
        //if not, create it
        File.Create(folderPath + fileName);
    }

    using(StreamWriter sw = new StreamWriter(folderPath + fileName))
    {
        foreach(var type in dataList)
        {
            sw.WriteLine(type.ToString());
        }
    }
}

我现在想要创建一个函数来将数据读回到指定泛型集合类型的实例中,但我正在努力解决这个问题。我目前的想法是该方法必须执行以下操作:

  1. 将文本文件拆分为行,然后将这些行拆分为各个数据类型的字符串。
  2. 使用反射来确定数据类型的结构
  3. 将字符串转换为2中标识的类型(根据指定泛型集合的格式)
  4. 到目前为止,我有以下内容:

    public static T LoadDataListFromFile<T>(string filePath) where T : IEnumerable, ICollection
    {
        //1. Text File to String Representations of Data Types
        char[] splitters = new char[]
        {
            ',', '-'
        };
    
        List<string[]> dataStrings = new List<string[]>();
    
        //Check to see if file already exists
        if(File.Exists(filePath))
        {
            using(StreamReader sr = new StreamReader(filePath))
            {
                //Seperate function I've written to calc num lines in a text file
                int numLines = NumLines(filePath);
    
                for(int i = 0; i < numLines; i++)
                {
                    string line = sr.ReadLine();
    
                    //remove all of the save formatting from the line(*Possibly do this on save)
                    line = line.Replace("[", "");
                    line = line.Replace("]", "");
                    line = line.Replace(" ", "");
    
                    //split the line as per the specified data splitters
                    string[] splitline = line.Split(splitters);
    
                    //add the strings representing different data types from this line
                    dataStrings.Add(splitline);
                }               
            }
        }
        else
        {
            //if reading the file doesn't work
            throw new Exception("Cannot load file");
        }
    
        //2. Determining the datatypes and their structure
    
        //overall generic type
        Type tType = typeof(T);
    
        //individual parameter types of generic type
        Type[] tTypes = tType.GetGenericArguments();
    
        //need to get constructor of types, then instance them, passing the string parameters(converted to their respective types) as parameters for the constructor 
    
        //for each of those parameter types
        List<object> parameterInstances = new List<object>();
    
        foreach(Type t in tTypes)
        {
            //create an instance of this particular type, with its parameters
            parameterInstances.Add(Activator.CreateInstance(t, BindingFlags.CreateInstance));
        }
    
        Debug.Log(parameterInstances[0]);
    
        //3. Construct the result
    
        //Create an instance of the overall type and populate it with the results
        var result = Activator.CreateInstance(tType, BindingFlags.CreateInstance);
    
        return (T)result;
    }
    

    我现在有点卡住,所以如果有人可以提供帮助,或指出我的方向,那将不胜感激。我知道这是一个非常通用和开放的请求,所以任何帮助都表示赞赏。

    谢谢, 马特

0 个答案:

没有答案