public static void PatchResourceManager(Type resourceManagerType) { if (resourceManagerType.GetProperty("ResourceManager", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public) != null) { try { var resManager = resourceManagerType.InvokeMember("ResourceManager", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.Public, null, resourceManagerType, new object[] { }); var fileName = resourceManagerType.Name + ".resx"; var databaseResourceManager = new DBResourceManager(fileName, resManager as ResourceManager); resourceManagerType.InvokeMember("resourceMan", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.SetField, null, resourceManagerType, new object[] { databaseResourceManager }); } catch (TargetInvocationException e) { if (e.InnerException is FileNotFoundException && ((FileNotFoundException)e.InnerException).FileName == "App_GlobalResources") { // ignore } else { throw; } } } }
public static void PatchAssembly(IOptionsMonitor <ILog> option, Assembly a, bool onlyAsc = true) { var log = option.CurrentValue; if (!onlyAsc || Accept(a)) { var types = new Type[0]; try { types = a.GetTypes(); } catch (ReflectionTypeLoadException rtle) { log.WarnFormat("Can not GetTypes() from assembly {0}, try GetExportedTypes(), error: {1}", a.FullName, rtle.Message); foreach (var e in rtle.LoaderExceptions) { log.Info(e.Message); } try { types = a.GetExportedTypes(); } catch (Exception err) { log.ErrorFormat("Can not GetExportedTypes() from assembly {0}: {1}", a.FullName, err.Message); } } foreach (var type in types) { var prop = type.GetProperty("ResourceManager", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); if (prop != null) { var rm = (ResourceManager)prop.GetValue(type); if (!(rm is DBResourceManager)) { var dbrm = new DBResourceManager(rm.BaseName, a); type.InvokeMember("resourceMan", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.SetField, null, type, new object[] { dbrm }); } } } } }