/// <summary> /// Dynamically instantiates all features in a given namespaceSuffix /// </summary> /// <param name="assembly">Assembly containing the feature types</param> /// <param name="namespaceSuffix">Namespace containing the feature types</param> /// <returns>Instances of all features in the namespace with the given suffix</returns> public static IEnumerable <CompiledFeature> LoadFeaturesFromNamespace(Assembly assembly, string namespaceSuffix) { var loadedFeatures = new List <CompiledFeature>(); if (assembly == null || string.IsNullOrEmpty(namespaceSuffix)) { return(loadedFeatures); } var featureScope = FeatureScope.Undefined; var featureTypes = assembly.GetTypes().Where(t => t.Namespace != null && t.Namespace.EndsWith(namespaceSuffix)); foreach (var featureType in featureTypes) { var featureMetadata = new CompiledFeatureMetadata { Name = featureType.Name, ClassName = featureType.Name }; try { var loadedFeature = CompiledFeatureFactory.GetInstance(featureScope, assembly, featureType.Namespace, featureMetadata); loadedFeatures.Add(loadedFeature); } catch (Exception ex) { Logger.LogError(ex, ex.Message); } } return(loadedFeatures); }
private static HashSet <CompiledFeature> LoadCompiledFeaturesByNamespace( FeatureScope featureScope, Assembly assembly, CompiledFeatureNamespace featureNamespace) { var loadedFeatures = new HashSet <CompiledFeature>(); foreach (var featureMetadata in featureNamespace.CompiledFeatureMetadata) { try { var compiledFeature = CompiledFeatureFactory.GetInstance(featureScope, assembly, featureNamespace.Namespace, featureMetadata); loadedFeatures.Add(compiledFeature); } catch (ClassNotFoundException e) { Logger.LogError(e, e.Message); } catch (InvalidFeatureException e) { Logger.LogError(e, e.Message); } } return(loadedFeatures); }
public void GetInstance_Throws_InvalidFeatureException_If_Type_Is_Not_A_Feature2() { var notAFeatureType = typeof(StringBuilder); var featureName = "SomeName"; var featureScope = FeatureScope.Undefined; Assert.Throws <InvalidFeatureException>(() => CompiledFeatureFactory.GetInstance(notAFeatureType, featureName, featureScope)); }
public void GetInstance_Returns_Feature_From_Type() { var featureType = typeof(AspNetMvcFeature); var feature = CompiledFeatureFactory.GetInstance(featureType); Assert.NotNull(feature); Assert.True(feature is AspNetMvcFeature); }
public void GetInstance_Returns_Feature_From_Type_With_Attributes() { var featureType = typeof(AspNetMvcFeature); var featureName = "FeatureName"; var featureScope = FeatureScope.Project; var feature = CompiledFeatureFactory.GetInstance(featureType, featureName, featureScope); Assert.NotNull(feature); Assert.True(feature.Name == featureName); Assert.True(feature.FeatureScope == featureScope); Assert.True(feature is AspNetMvcFeature); }
public void GetInstance_Throws_InvalidFeatureException_If_Type_Is_Not_A_Feature3() { var notAFeatureType = typeof(StringBuilder); var featureMetadata = new CompiledFeatureMetadata { Name = "SomeName", ClassName = notAFeatureType.Name }; var featureScope = FeatureScope.Undefined; var assembly = Assembly.GetAssembly(notAFeatureType); var @namespace = notAFeatureType.Namespace; Assert.Throws <InvalidFeatureException>(() => CompiledFeatureFactory.GetInstance(featureScope, assembly, @namespace, featureMetadata)); }
public void GetInstance_Returns_Feature_From_Assembly() { var featureType = typeof(AspNetMvcFeature); var assembly = Assembly.GetAssembly(featureType); var featureScope = FeatureScope.Project; var @namespace = featureType.Namespace; var featureName = "FeatureName"; var className = nameof(AspNetMvcFeature); var metadata = new CompiledFeatureMetadata { Name = featureName, ClassName = className }; var feature = CompiledFeatureFactory.GetInstance(featureScope, assembly, @namespace, metadata); Assert.NotNull(feature); Assert.True(feature.Name == featureName); Assert.True(feature.FeatureScope == featureScope); Assert.True(feature is AspNetMvcFeature); }
public void GetInstance_Throws_InvalidFeatureException_If_Type_Is_Not_A_Feature1() { var notAFeatureType = typeof(StringBuilder); Assert.Throws <InvalidFeatureException>(() => CompiledFeatureFactory.GetInstance(notAFeatureType)); }
/// <summary> /// Dynamically instantiates a CompiledFeature by its type /// </summary> /// <param name="type">CompiledFeature type to instantiate</param> /// <returns>CompiledFeature instance</returns> public static CompiledFeature LoadFeaturesByType(Type type) { return(CompiledFeatureFactory.GetInstance(type)); }