The typical lifecycle for a factory consumer is:
private AbstractAnalysisFactory AnalysisFactory(Type clazz, Version matchVersion, IResourceLoader loader, params string[] keysAndValues) { if (keysAndValues.Length % 2 == 1) { throw new System.ArgumentException("invalid keysAndValues map"); } string previous; IDictionary <string, string> args = new Dictionary <string, string>(); for (int i = 0; i < keysAndValues.Length; i += 2) { if (args.TryGetValue(keysAndValues[i], out previous)) { fail("duplicate values for key: " + keysAndValues[i]); } args[keysAndValues[i]] = keysAndValues[i + 1]; } if (args.TryGetValue("luceneMatchVersion", out previous)) { fail("duplicate values for key: luceneMatchVersion"); } args["luceneMatchVersion"] = matchVersion.ToString(); AbstractAnalysisFactory factory = null; try { factory = (AbstractAnalysisFactory)Activator.CreateInstance(clazz, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { args }, CultureInfo.InvariantCulture); } catch (TargetInvocationException e) { // to simplify tests that check for illegal parameters if (e.InnerException is System.ArgumentException) { throw (System.ArgumentException)e.InnerException; } else { throw e; } } if (factory is IResourceLoaderAware) { ((IResourceLoaderAware)factory).Inform(loader); } return(factory); }
private AbstractAnalysisFactory AnalysisFactory(Type clazz, Version matchVersion, IResourceLoader loader, params string[] keysAndValues) { if (keysAndValues.Length % 2 == 1) { throw new ArgumentException("invalid keysAndValues map"); } string previous; IDictionary <string, string> args = new Dictionary <string, string>(); for (int i = 0; i < keysAndValues.Length; i += 2) { if (args.TryGetValue(keysAndValues[i], out previous)) { fail("duplicate values for key: " + keysAndValues[i]); } args[keysAndValues[i]] = keysAndValues[i + 1]; } if (args.TryGetValue("luceneMatchVersion", out previous)) { fail("duplicate values for key: luceneMatchVersion"); } args["luceneMatchVersion"] = matchVersion.ToString(); AbstractAnalysisFactory factory = null; try { factory = (AbstractAnalysisFactory)Activator.CreateInstance(clazz, args); } catch (Exception e) when(e.IsInvocationTargetException()) { // to simplify tests that check for illegal parameters if (e.InnerException is ArgumentException argumentException) { ExceptionDispatchInfo.Capture(argumentException).Throw(); // LUCENENET: Rethrow to preserve stack details from the original throw } else { throw; // LUCENENET: CA2200: Rethrow to preserve stack details (https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200-rethrow-to-preserve-stack-details) } } if (factory is IResourceLoaderAware resourceLoaderAware) { resourceLoaderAware.Inform(loader); } return(factory); }