static DataAdapterReflector() { string targetPath = DataAdapterConfigReader.GetDefualtDataAdapterFilePath(); _defaultAssembly = LoadAssembly(targetPath); _updater = new AdapterAutoUpdater(); }
/// <summary> /// /// </summary> public AdapterAutoUpdater() { _allAdapterFiles = DataAdapterConfigReader.GetAdapterFiles(); List <string> targetDir = GetTargetDir(_allAdapterFiles); foreach (string dir in targetDir) { FileSystemWatcher watcher = new FileSystemWatcher(dir); watcher.EnableRaisingEvents = true; watcher.Changed += new FileSystemEventHandler(watcher_Changed); } }
/// <summary> /// 创建指定类型接口的实现类对象. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static T CreateInstance <T>() { Type interfaceType = typeof(T); string interfaceName = interfaceType.FullName; Type tg = null; lock (_asycObj) { var typeInfo = _lstClassTypeInfo.FirstOrDefault(c => c.InterfaceName == interfaceName); if (typeInfo != null) { tg = typeInfo.Type; } else { string targetFileName = DataAdapterConfigReader.GetAdapterFilePath(interfaceName); Assembly asm = null; if (targetFileName != null) { if (_dicInterfaceAssembly.ContainsKey(targetFileName)) { asm = _dicInterfaceAssembly[targetFileName]; } else { if (!File.Exists(targetFileName)) { string message = string.Format("无法找到文件:{0}。", targetFileName); _logger.Error(message); throw new Exception(message); } asm = LoadAssembly(targetFileName); _dicInterfaceAssembly[targetFileName] = asm; } } else { asm = _defaultAssembly; } string specialAdapter = DataAdapterConfigReader.FindSpecialAdapterType(interfaceName); tg = FindImplementType(asm, interfaceType, tg, specialAdapter); _lstClassTypeInfo.Add( new AdapterClassTypeInfo() { FilePath = targetFileName, InterfaceName = interfaceName, Type = tg }); } } if (tg != null) { return((T)Activator.CreateInstance(tg)); } else { string message = string.Format("无法找到接口:{0} 的适配器", interfaceName); _logger.Error(message); throw new Exception(message); } }