Beispiel #1
0
 protected void LoadData(string path, LoadDataDelegate callback)
 {
     using (StreamReader sr = new StreamReader(@path))
     {
         while (!sr.EndOfStream)
         {
             string line = sr.ReadLine();
             callback(line);
         }
     }
 }
        public static void InitializeJsonData(Assembly dataAssembly, LoadDataDelegate loadData)
        {
            foreach (var type in dataAssembly.GetTypes())
            {
                string className = type.Name;
                //跳过非索引器类型,需要表配置Key
                if (!className.EndsWith("Indexer"))
                {
                    continue;
                }

                className = className.Substring(0, className.Length - 7);

                var insField = type.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static);
                if (insField == null)
                {
                    continue;
                }
                var indexer = insField.GetGetMethod().Invoke(null, null);
                Debug.Assert(indexer != null, "indexer null");
                byte[] bytes = loadData(className);
                if (bytes == null || bytes.Length == 0)
                {
                    continue;
                }
                string json = Encoding.UTF8.GetString(bytes);

                if (string.IsNullOrEmpty(json))
                {
                    continue;
                }
                json = "{\"array\":" + json + "}";
                Type dataType;

                dataType = type.BaseType.GetGenericArguments()[1];

                var array = ArrayFromJson(json, dataType);
                if (array == null)
                {
                    continue;
                }

                var InitializeMethod = indexer.GetType().GetMethod("Initialize", BindingFlags.Public | BindingFlags.Instance)
                                       .Invoke(indexer, new object[] { array });
            }
        }