Exemple #1
0
        public List <TResult> TextFileDeserialize <TResult>() where TResult : new()
        {
            List <TResult> deserializedCollection = new List <TResult>();
            string         fullPath   = $"{Directory.GetCurrentDirectory().Replace("\\bin\\Debug\\netcoreapp2.0", "")}\\TextFiles\\accounts.txt";
            List <string>  fileResult = File.ReadAllLines(fullPath).ToList();

            for (int i = 0; i < fileResult.Count; i++)
            {
                TResult deserializedObject = new TResult();
                int     currentIndex       = 0;
                string  currentLine        = fileResult[i];
                bool    isLoopEnd          = false;
                while (!isLoopEnd)
                {
                    int    stopIndex  = currentLine.IndexOf('|', 0);
                    string tempObject = currentLine.Substring(0, stopIndex);
                    stopIndex = tempObject.IndexOfAny(new char[] { ':', '[' });
                    //if(tempObject[stopIndex] == '[')
                    string propName  = tempObject.Substring(0, stopIndex);
                    string propValue = tempObject.Substring(stopIndex + 1);
                    DeserializeCollection(ref deserializedObject, propName, propValue);
                    PropertyInfo prop           = deserializedObject.GetType().GetProperty(propName);
                    var          convertedValue = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromInvariantString(propValue);
                    prop.SetValue(deserializedObject, convertedValue);
                    currentLine = currentLine.Remove(0, tempObject.Length + 1);
                    if (currentLine.Length == 0)
                    {
                        isLoopEnd = true;
                    }
                }
            }
            return(deserializedCollection);
        }
Exemple #2
0
        public MultiResultSet With <TResult>()
        {
            _resultSets.Add((reader) =>
            {
                IList <TResult> result = new List <TResult>();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        TResult obj = Activator.CreateInstance <TResult>();
                        foreach (PropertyInfo item in obj.GetType().GetProperties())
                        {
                            item.SetValue(obj, reader[item.Name].GetType() == typeof(DBNull) ? null : reader[item.Name], null);
                        }
                        result.Add(obj);
                    }
                }
                return(result);
            });

            return(this);
        }