private static void RegisterRepositories() { var types = typeof(TContext).Assembly.GetTypes(); var repoApis = from type in types let genericApi = type.GetInterfaces().FirstOrDefault(i => i.GetGenericArguments().Length == 1 && typeof(IRepository <>) == i.GetGenericTypeDefinition()) where type.IsInterface && genericApi != null select new { RepoApi = type, GenericApi = genericApi }; foreach (var apiPair in repoApis) { Type repoProxy; // Find implementations for the RepoAPI, which will also fit the generic API because of the inheritance var implementations = types.Where(t => t.IsClass && apiPair.RepoApi.IsAssignableFrom(t)).ToList(); if (implementations.Count == 0) { repoProxy = ProxyBuilder.Build(apiPair.RepoApi); } else { var selectedImpl = implementations.First(); repoProxy = ProxyBuilder.Build(apiPair.RepoApi, selectedImpl); } var constructorDelegate = ReflectionTool.ConstructorDelegate <Repository>(repoProxy); // Register constructor for both interfaces Repositories[apiPair.RepoApi] = Repositories[apiPair.GenericApi] = constructorDelegate; } }
/// <summary> /// Resolve the binding parameters /// </summary> /// <param name="process">Process to bind to</param> /// <returns>Parameters with resolved bindings</returns> protected sealed override ParametersBase ResolveBinding(IProcess process) { if (_instanceDelegate == null) { _instanceDelegate = ReflectionTool.ConstructorDelegate <Parameters>(GetType()); } var instance = _instanceDelegate(); Populate(process, instance); return(instance); }
/// <summary> /// Start the storage and load the type strategies /// </summary> public void Start() { _types = ReflectionTool.GetPublicClasses <ProductType>().ToDictionary(t => t.Name, t => t); // Create type strategies foreach (var config in Config.TypeStrategies) { var strategy = StrategyFactory.CreateTypeStrategy(config); TypeStrategies[config.TargetType] = strategy; TypeConstructors[config.TargetType] = ReflectionTool.ConstructorDelegate <ProductType>(strategy.TargetType); } // Create instance strategies foreach (var config in Config.InstanceStrategies) { var strategy = StrategyFactory.CreateInstanceStrategy(config); InstanceStrategies[config.TargetType] = strategy; } // Create link strategies foreach (var config in Config.LinkStrategies) { var strategy = StrategyFactory.CreateLinkStrategy(config); LinkStrategies[config.TargetType][config.PartName] = strategy; var property = strategy.TargetType.GetProperty(config.PartName); var linkType = property.PropertyType; // Extract element type from collections if (typeof(IEnumerable <IProductPartLink>).IsAssignableFrom(linkType)) { linkType = linkType.GetGenericArguments()[0]; } // Build generic type if (linkType.IsInterface && linkType.IsGenericType) { var genericElement = linkType.GetGenericArguments()[0]; linkType = typeof(ProductPartLink <>).MakeGenericType(genericElement); } LinkConstructors[$"{config.TargetType}.{config.PartName}"] = ReflectionTool.ConstructorDelegate <IProductPartLink>(linkType); } // Create recipe strategies foreach (var config in Config.RecipeStrategies) { var strategy = StrategyFactory.CreateRecipeStrategy(config); RecipeStrategies[config.TargetType] = strategy; RecipeConstructors[config.TargetType] = ReflectionTool.ConstructorDelegate <IProductRecipe>(strategy.TargetType); } }
public void CreateConstructor(Type targetType) { // Arrange var target = typeof(BaseClass); // Act var func1 = ReflectionTool.ConstructorDelegate(target); var func2 = ReflectionTool.ConstructorDelegate <BaseClass>(targetType); var result1 = func1(); var result2 = func2(); // Assert Assert.NotNull(result1); Assert.NotNull(result2); Assert.IsInstanceOf <BaseClass>(result1); }