private Assembly loadAssembly(string fullPath, CacheResultCollectorBase collector, bool fatalErrors = false) { collector.StartedAssemblyLoad(fullPath); try { return(Assembly.LoadFrom(fullPath)); } catch (Exception ex) { if (fatalErrors) { throw new FailureException("Failed to load " + fullPath, ex); } collector.Warning("Failed to load " + fullPath + ": " + ex); return(null); } }
public void ReadCacheFiles(CacheResultCollectorBase collector) { try { var baseDir = AppDomain.CurrentDomain.BaseDirectory; var nrdo = loadAssembly(Path.Combine(baseDir, "NR.nrdo.dll"), collector, true); var tablesAttrType = getType(nrdo, "NR.nrdo.Attributes.NrdoTablesAttribute"); var queriesAttrType = getType(nrdo, "NR.nrdo.Attributes.NrdoQueriesAttribute"); var getTableType = getProperty <Type>(tablesAttrType, "Type"); var getQueryType = getProperty <Type>(queriesAttrType, "Type"); var objectBaseAttrType = getType(nrdo, "NR.nrdo.Attributes.NrdoObjectTypeAttribute"); var getCacheFileName = getProperty <string>(objectBaseAttrType, "CacheFileName"); var getCacheFileContents = getProperty <string>(objectBaseAttrType, "CacheFileContents"); var tableAttrType = getType(nrdo, "NR.nrdo.Attributes.NrdoTableAttribute"); var queryAttrType = getType(nrdo, "NR.nrdo.Attributes.NrdoQueryAttribute"); foreach (var file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll")) { var asm = loadAssembly(file, collector); if (asm == null) { continue; } foreach (var attr in asm.GetCustomAttributes(tablesAttrType, false)) { var tableType = getTableType(attr); var tableAttr = tableType.GetCustomAttributes(tableAttrType, false).SingleOrDefault(); if (tableAttr == null) { collector.Warning("Did not find " + tableAttrType.Name + " on " + tableType.Name); } else { collector.FoundCacheFile(getCacheFileName(tableAttr), getCacheFileContents(tableAttr)); } } foreach (var attr in asm.GetCustomAttributes(queriesAttrType, false)) { var queryType = getQueryType(attr); var queryAttr = queryType.GetCustomAttributes(queryAttrType, false).SingleOrDefault(); if (queryAttr == null) { collector.Warning("Did not find " + queryAttrType.Name + " on " + queryType.Name); } else { collector.FoundCacheFile(getCacheFileName(queryAttr), getCacheFileContents(queryAttr)); } } } } catch (Exception ex) { if (ex is FailureException) { var msg = ex.Message; if (ex.InnerException != null) { msg += ": " + ex.InnerException; } collector.DidNotFindCacheAttributes(msg); } else { collector.DidNotFindCacheAttributes("Unknown error: " + ex); } } }