コード例 #1
0
        public void LoadTables()
        {
            var tables        = RecordsAssembly.GetTypes().Where(x => x.GetInterface("ITable") != null).ToArray();
            var orderedTables = new Type[tables.Length];
            var dontCatch     = new List <Type>();

            foreach (var table in tables)
            {
                var attribute = (TableAttribute)table.GetCustomAttribute(typeof(TableAttribute), false);
                if (attribute == null)
                {
                    logger.Write(string.Format("Warning : the table type '{0}' hasn't got an attribute called 'TableAttribute'", table.Name), MessageState.WARNING);
                    continue;
                }

                if (attribute.catchAll)
                {
                    if (attribute.readingOrder >= 0)
                    {
                        orderedTables[attribute.readingOrder] = table;
                    }
                }
                else
                {
                    dontCatch.Add(table);
                }
            }
            foreach (var table in tables)
            {
                if (orderedTables.Contains(table) || dontCatch.Contains(table))
                {
                    continue;
                }

                for (var i = tables.Length - 1; i >= 0; i--)
                {
                    if (orderedTables[i] == null)
                    {
                        orderedTables[i] = table;
                        break;
                    }
                }
            }
            foreach (var type in orderedTables)
            {
                if (type == null)
                {
                    continue;
                }

                var reader    = Activator.CreateInstance(typeof(DatabaseReader <>).MakeGenericType(type));
                var tableName = (string)reader.GetType().GetProperty("TableName").GetValue(reader);


                logger.Write("Loading " + tableName + " ...");
                var method = reader.GetType().GetMethods().FirstOrDefault(x => x.Name == "Read" && x.GetParameters().Length == 1);
                method.Invoke(reader, new object[] { this.UseProvider() });

                var elements = reader.GetType().GetProperty("Elements").GetValue(reader);

                var field = ITableManager.GetCache(type);

                if (field != null)
                {
                    field.FieldType.GetMethod("AddRange").Invoke(field.GetValue(null), new object[] { elements });;
                }
            }
        }