Exemplo n.º 1
0
        /// <summary>
        /// 映射数据源中的数据到目标对象
        /// </summary>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List <TDestination> List <TDestination>(object source)
        {
            var accessor = GetAccessor <TDestination>();

            if (accessor == null)
            {
                throw new MapperException(string.Format("未找到适用{0}的对象访问器。", typeof(TDestination).FullName));
            }

            var typeMapper = NativeTypeMapper.GetTypeMapper <TDestination>();

            var dataSourceReader = NativeDataSourceReader.GetReader(source);
            var fields           = accessor.MetaInfo.GetPropertyNames();

            dataSourceReader.Ordinal(fields);

            var list = new List <TDestination>(dataSourceReader.Count > 0 ? dataSourceReader.Count : 8);

            object[] initData;
            while (null != (initData = dataSourceReader.Read()))
            {
                TDestination destObj = typeMapper.Cast(initData);
                if (destObj != null)
                {
                    list.Add(destObj);
                }
            }
            return(list);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 映射数据源中的一行数据到目标对象
        /// </summary>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static TDestination Single <TDestination>(object source)
        {
            var accessor = GetAccessor <TDestination>();

            if (accessor == null)
            {
                throw new MapperException(string.Format("未找到适用{0}的对象访问器。", typeof(TDestination).FullName));
            }

            var dataSourceReader = NativeDataSourceReader.GetReader(source);
            var fields           = accessor.MetaInfo.GetPropertyNames();

            dataSourceReader.Ordinal(fields);

            var initData = dataSourceReader.Read();

            if (initData != null)
            {
                return(NativeTypeMapper.GetTypeMapper <TDestination>().Cast(initData));
            }

            return(default(TDestination));
        }