Exemplo n.º 1
0
    ////从文件初始化关卡信息(Level)
    //private void InitLevelModel(ref Dictionary<string, LevelModel> _levelModel, string _path)
    //{
    //    if (_levelModel == null)
    //    {
    //        _levelModel = new Dictionary<int, LevelModel>();
    //    }
    //    RDFileStream.ReadLevelTable(ref _levelModel, _path);
    //}

    //从CSV表初始化Dictionary
    private static Dictionary <string, T> LoadCsvData <T>(string _fileName)
    {
        Dictionary <string, T> dic = new Dictionary <string, T>();

        /* 从CSV文件读取数据 */
        Dictionary <string, Dictionary <string, string> > result = RDFileStream.ReadCsvFile(_fileName);

        /* 遍历每一行数据 */
        foreach (string name in result.Keys)
        {
            /* CSV的一行数据 */
            Dictionary <string, string> datas = result[name];

            /* 读取Csv数据对象的属性 */
            PropertyInfo[] props = typeof(T).GetProperties();
            /* 使用反射,将CSV文件的数据赋值给CSV数据对象的相应字段,要求CSV文件的字段名和CSV数据对象的字段名完全相同 */
            T obj = Activator.CreateInstance <T>();
            foreach (PropertyInfo p in props)
            {
                ReflectUtil.PiSetValue <T>(datas[p.Name], p, obj);
            }

            /* 按name-数据的形式存储 */
            dic[name] = obj;
        }

        return(dic);
    }
Exemplo n.º 2
0
    /// <summary>
    /// 读取CSV文件数据(利用反射)
    /// </summary>
    /// <typeparam name="CsvData">CSV数据对象的类型</typeparam>
    /// <param name="csvFilePath">CSV文件路径</param>
    /// <param name="csvDatas">用于缓存数据的字典</param>
    /// <returns>CSV文件所有行内容的数据对象</returns>
    private static Dictionary <int, T_CsvData> LoadCsvData <T_CsvData>(string csvFilePath)
    {
        Dictionary <int, T_CsvData> dic = new Dictionary <int, T_CsvData>();

        /* 从CSV文件读取数据 */
        Dictionary <string, Dictionary <string, string> > result = CsvLoader.LoadCsvFile(csvFilePath);

        /* 遍历每一行数据 */
        foreach (string ID in result.Keys)
        {
            /* CSV的一行数据 */
            Dictionary <string, string> datas = result[ID];

            /* 读取Csv数据对象的属性 */
            PropertyInfo[] props = typeof(T_CsvData).GetProperties();

            /* 使用反射,将CSV文件的数据赋值给CSV数据对象的相应字段,要求CSV文件的字段名和CSV数据对象的字段名完全相同 */
            T_CsvData obj = Activator.CreateInstance <T_CsvData>();
            foreach (PropertyInfo p in props)
            {
                ReflectUtil.PiSetValue <T_CsvData>(datas[p.Name], p, obj);
            }

            /* 按ID-数据的形式存储 */
            dic[Convert.ToInt32(ID)] = obj;
        }

        return(dic);
    }