Пример #1
0
        public static object ToObject(object source, Type targetType)
        {
            IMapDataSource dataSource = GetDataSource(source);

            object targetObject = Activator.CreateInstance(targetType);

            if (targetObject != null && dataSource != null)
            {
                TypeDescriptor targetDescriptor = TypeDescriptorFactory.CreateTypeDescriptor(targetType) as TypeDescriptor;

                ArrayList dataDescList = GetTypeMappingList(targetDescriptor, dataSource);

                //The Source has to be passed as the last parameter in ToObjectInternal call!
                targetObject = ToObjectInternal(targetObject, dataSource, dataDescList, source);
            }

            return(targetObject);
        }
Пример #2
0
        public static T ToObject <T>(object source) where T : new()
        {
            Type targetType = typeof(T);

            IMapDataSource dataSource = GetDataSource(source);

            T targetObject = new T(); //Activator.CreateInstance(targetType);

            if (targetObject != null && dataSource != null)
            {
                TypeDescriptor targetDescriptor = TypeDescriptorFactory.CreateTypeDescriptor(targetType) as TypeDescriptor;

                ArrayList dataDescList = GetTypeMappingList(targetDescriptor, dataSource);

                //The Source has to be passed as the last parameter in ToObjectInternal call!
                targetObject = ToObjectInternal(targetObject, dataSource, dataDescList, source);
            }

            return(targetObject);
        }
Пример #3
0
        public static object ToObject(object source, object targetObject)
        {
            IMapDataSource dataSource = GetDataSource(source);

            if (targetObject != null && dataSource != null)
            {
                if ((targetObject is IMapDataSource) && (((IMapDataSource)targetObject).IsCollection() == true))
                {
                    targetObject = ToObjectInternal(targetObject, dataSource, source, true);
                }
                else
                {
                    Type targetType = targetObject.GetType();

                    TypeDescriptor targetDescriptor = TypeDescriptorFactory.CreateTypeDescriptor(targetType) as TypeDescriptor;

                    ArrayList dataDescList = GetTypeMappingList(targetDescriptor, dataSource);

                    targetObject = ToObjectInternal(targetObject, dataSource, dataDescList, source);
                }
            }

            return(targetObject);
        }
Пример #4
0
        private static IMapDataSource GetDataSource(object obj)
        {
            if (obj is IMapDataSource)
            {
                return((IMapDataSource)obj);
            }

            if (obj is DataRow)
            {
                return(new DataRowReader((DataRow)obj));
            }

            if (obj is DataTable)
            {
                return(new DataRowReader(((DataTable)(obj)).Rows[0]));
            }

            if (obj is IDataReader)
            {
                return(new DataReaderSource((IDataReader)obj));
            }

            return(TypeDescriptorFactory.CreateTypeDescriptor(obj.GetType()));
        }