Пример #1
0
        /// <summary>
        /// async stream not supported at current version ,
        /// when new version released ,do a change
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async Task <IEnumerable <T> > ReadToEndAsync <T>()
        {
            string line;

            var type = typeof(T);

            var ls = new List <T>();

            while (true)
            {
                line = await _reader.ReadLineAsync();

                if (line == null)
                {
                    break;
                }

                line = line.Trim();

                var records = line.Splite(",");

                var resolveItems = createResolveItems(type);

                var instance = _reflection.GetItem(type)
                               .Constructors.GetDefaultConstructor()
                               ?.DefaultInvoker
                               ?.Invoke();

                if (instance == null)
                {
                    throw new DefaultConstructorNotFoundException(type);
                }

                foreach (var item in resolveItems)
                {
                    if (item.Column > records.Count)
                    {
                        throw new ArgumentOutOfRangeException();
                    }

                    if (!item.Property.CanWrite)
                    {
                        throw new Exception();
                    }

                    item.Property.Setter.Invoke(instance,
                                                JasmineStringValueConvertor.FromString(records[item.Column], item.Property.PropertyType));
                }

                ls.Add((T)instance);
            }

            return(ls);
        }
Пример #2
0
        public IEnumerable <T> Read <T>(int count)
        {
            string line;

            var type = typeof(T);
            var t    = 0;

            while (++t < count)
            {
                line = _reader.ReadLine()?.Trim();

                if (line == null)
                {
                    break;
                }

                var records = line.Splite(",");

                var resolveItems = createResolveItems(type);

                var instance = _reflection.GetItem(type)
                               .Constructors.GetDefaultConstructor()
                               ?.DefaultInvoker
                               ?.Invoke();

                if (instance == null)
                {
                    throw new DefaultConstructorNotFoundException(type);
                }

                foreach (var item in resolveItems)
                {
                    if (item.Column > records.Count)
                    {
                        throw new ArgumentOutOfRangeException();
                    }

                    if (!item.Property.CanWrite)
                    {
                        throw new Exception();
                    }

                    item.Property.SetValue(instance,
                                           JasmineStringValueConvertor.FromString(records[item.Column], item.Property.PropertyType));
                }

                yield return((T)instance);
            }
        }
Пример #3
0
        public dynamic ReadOne(Type type)
        {
            string line;

            do
            {
                line = _reader.ReadLine()?.Trim();
            } while (line != string.Empty);


            if (line == null)
            {
                return(null);
            }

            var records = line.Splite(",");

            var resolveItems = createResolveItems(type);

            var instance = _reflection.GetItem(type)
                           .Constructors
                           .GetDefaultConstructor()
                           ?.DefaultInvoker
                           ?.Invoke();

            if (instance == null)
            {
                throw new DefaultConstructorNotFoundException(type);
            }

            foreach (var item in resolveItems)
            {
                if (item.Column > records.Count - 1)
                {
                    throw new ArgumentOutOfRangeException();
                }

                if (!item.Property.CanWrite)
                {
                    throw new Exception();
                }

                item.Property.SetValue(instance,
                                       JasmineStringValueConvertor.FromString(records[item.Column], item.Property.PropertyType));
            }

            return(instance);
        }
 public static object Convert(Type type, string source)
 {
     return(type.IsBaseType() ? JasmineStringValueConvertor.FromString(source, type) :
            Newtonsoft.Json.JsonConvert.DeserializeObject(source, type));
 }