コード例 #1
0
ファイル: EntityExtensions.cs プロジェクト: githuer/DMSFrame
        /// <summary>
        /// 根据MAPPING创建表
        /// </summary>
        /// <param name="entityType"></param>
        /// <returns></returns>
        public static ConstructorInfo GetEntityMappingConstructorInfo(this Type entityType)
        {
            ConstructorInfo dataTable = null;
            IDictionary <Type, ConstructorInfo> entityMappingTableCache;

            Monitor.Enter(entityMappingTableCache = EntityExtensions.EntityMappingConstructorInfoCache);
            try
            {
                if (EntityExtensions.EntityMappingConstructorInfoCache.Keys.Contains(entityType))
                {
                    dataTable = EntityExtensions.EntityMappingConstructorInfoCache[entityType];
                    return(dataTable);
                }
                dataTable = EntityExtensions.CreateEntityMappingConstructorInfo(entityType);
                EntityExtensions.EntityMappingConstructorInfoCache[entityType] = dataTable;
            }
            catch
            { }
            finally
            {
                Monitor.Exit(EntityMappingConstructorInfoCache);
            }
            return(dataTable);
        }
コード例 #2
0
ファイル: EntityExtensions.cs プロジェクト: githuer/DMSFrame
        /// <summary>
        /// 根据MAPPING创建表
        /// </summary>
        /// <param name="entityType"></param>
        /// <returns></returns>
        public static DataTable GetEntityMappingTable(this Type entityType)
        {
            DataTable dataTable = null;
            IDictionary <Type, DataTable> entityMappingTableCache;

            Monitor.Enter(entityMappingTableCache = EntityExtensions.EntityMappingTableCache);
            try
            {
                if (EntityExtensions.EntityMappingTableCache.Keys.Contains(entityType))
                {
                    dataTable = EntityExtensions.EntityMappingTableCache[entityType];
                    return(dataTable.Clone());
                }
                dataTable = EntityExtensions.CreateEntityMappingTable(entityType);
                EntityExtensions.EntityMappingTableCache[entityType] = dataTable;
            }
            catch
            { }
            finally
            {
                Monitor.Exit(entityMappingTableCache);
            }
            return(dataTable.Clone());
        }
コード例 #3
0
ファイル: EntityExtensions.cs プロジェクト: githuer/DMSFrame
        /// <summary>
        /// LIST实体转换表格
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="entityList"></param>
        /// <returns></returns>
        public static DataTable ToDataTable <T>(this IEnumerable <T> entityList)
        {
            if (typeof(T).IsClass && typeof(T).IsGenericType)
            {
                Type      entityType         = typeof(T);
                DataTable entityMappingTable = EntityExtensions.GetEntityMappingTable(entityType);
                if (entityList == null || entityList.Count() == 0)
                {
                    return(entityMappingTable);
                }

                var constructor = entityType.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                                  .OrderBy(c => c.GetParameters().Length).First();
                //取当前构造函数的参数
                var             parameters = constructor.GetParameters();
                IEnumerator <T> enumerator = entityList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    T       current = enumerator.Current;
                    DataRow dataRow = entityMappingTable.NewRow();
                    foreach (var item in parameters)
                    {
                        PropertyInfo p = current.GetType().GetProperty(item.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                        if (p != null)
                        {
                            object obj = p.GetValue(current, null);
                            if (p.PropertyType.GetUnderlyingType() == typeof(DateTime))
                            {
                                obj = ((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss.fff");
                            }
                            dataRow[item.Name] = ((obj == null) ? DBNull.Value : obj);
                        }
                    }
                    enumerator.Disposed();
                    entityMappingTable.Rows.Add(dataRow);
                }
                return(entityMappingTable);
            }
            else
            {
                Type      entityType         = typeof(T);
                DataTable entityMappingTable = EntityExtensions.GetEntityMappingTable(entityType);
                if (entityList == null || entityList.Count() == 0)
                {
                    return(entityMappingTable);
                }
                IEnumerator <T> enumerator = entityList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    T current = enumerator.Current;


                    //DataRow dataRow = entityMappingTable.NewRow();
                    //IEnumerator enumerator2 = entityMappingTable.Columns.GetEnumerator();
                    //while (enumerator2.MoveNext())
                    //{
                    //    DataColumn dataColumn = (DataColumn)enumerator2.Current;
                    //    if (!dataColumn.AutoIncrement)
                    //    {
                    //        if (((IEntity)current).ChangedMappingProperties.Keys.Contains(dataColumn.ColumnName))
                    //        {
                    //            object obj = ((IEntity)current).ChangedMappingProperties[dataColumn.ColumnName];
                    //            dataRow[dataColumn.ColumnName] = ((obj == null) ? DBNull.Value : obj);
                    //        }
                    //    }
                    //}

                    DataRow     dataRow     = entityMappingTable.NewRow();
                    IEnumerator enumerator2 = entityMappingTable.Columns.GetEnumerator();
                    while (enumerator2.MoveNext())
                    {
                        DataColumn   dataColumn = (DataColumn)enumerator2.Current;
                        PropertyInfo p          = current.GetType().GetProperty(dataColumn.ColumnName, BindingFlags.Public | BindingFlags.Instance);
                        if (p != null)
                        {
                            object obj = p.GetValue(current, null);
                            if (p.PropertyType.GetUnderlyingType() == typeof(DateTime))
                            {
                                obj = ((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss.fff");
                            }
                            dataRow[dataColumn.ColumnName] = ((obj == null) ? DBNull.Value : obj);
                        }
                    }
                    enumerator.Disposed();
                    entityMappingTable.Rows.Add(dataRow);
                }
                enumerator.Disposed();
                return(entityMappingTable);
            }
        }