private void UseFactory(IMetricFactory factory) { for (int i = 0; i < factory.Count; i++) { // this copy is important as the lambda function below // would capture i, which equals factory.Count at the end of the loop. // Therefore the lambda would always try to create factory item #factory.Count // which does not exist. // Don't remove the index variable. int index = i; var info = factory.GetMetricInfo(index); var metricInfo = new MetricInfo( info.Name, info.Category, info.UniqueName, factory.Id, g => factory.CreateInstance(index, g), (g, x) => factory.CreateInstance(index, g, x) ); _metrics.Add(metricInfo); MetricAdded?.Invoke(this, new MetricAddedEventArgs(metricInfo)); } }
/// <summary> /// Add the type information of a metric to the metric collection. /// This allows the editor to instantiate it. /// </summary> /// <param name="type">type of the metric</param> /// <returns>true if the type is compatible (has a metric attribute and is instantiable)</returns> public bool TryRegisterMetric(Type type) { MetricAttribute metricAttrib = null; try { metricAttrib = (MetricAttribute)type.GetCustomAttribute(typeof(MetricAttribute)); } catch (Exception) { return(false); } if (metricAttrib == null) { return(false); } if (metricAttrib.Instantiable) { var metricInfo = new MetricInfo( metricAttrib.Name, metricAttrib.Category, type.FullName, _genericFactoryGuid, FactoryForType(type), FactoryXmlForType(type) ); _metrics.Add(metricInfo); MetricAdded?.Invoke(this, new MetricAddedEventArgs(metricInfo)); } return(true); }
private void NotifyMetricAdded( MetricName name, Metric metric) { MetricAdded?.Invoke(this, new MetricsRegistryEventArgs(name, metric)); }