/// <summary> /// Finds all classes marked as [Component] in the /// assembly, and stores them in the Container. /// </summary> /// <param name="runnable">Runnable class</param> internal static void InstanceComponents(ISnowRunnable runnable) { var type = runnable.GetType(); var assembly = type?.Assembly; if (assembly is null) { return; } Container.AllComponents = assembly.GetTypes() .Where(t => t.CustomAttributes.Any(ca => typeof(ComponentAttribute).IsAssignableFrom(ca.AttributeType))) .ToList(); // Also check referenced assemblies. foreach (var assemblyName in assembly.GetReferencedAssemblies()) { var dependency = Assembly.Load(assemblyName); Container.AllComponents = Container.AllComponents.Concat( dependency.GetTypes() .Where(t => t.CustomAttributes.Any(ca => typeof(ComponentAttribute).IsAssignableFrom(ca.AttributeType))) ).ToList(); } foreach (var component in Container.AllComponents) { InstanceComponent(component); } }
/// <summary> /// Finds all components in the assembly, and provides /// each occurrence of [Autowired] with an instance. /// </summary> /// <param name="runnable">Runnable class</param> internal static void InjectDependencies(ISnowRunnable runnable) { var type = runnable.GetType(); ReadyInstance(type, runnable); var assembly = type?.Assembly; if (assembly is null) { return; } ((IEnumerable <Type>)assembly.GetTypes()) .Where(t => t.CustomAttributes.Any(ca => typeof(ComponentAttribute).IsAssignableFrom(ca.AttributeType))) .ForEach(t => ReadyInstance(t, Container.Retrieve(t))); // Also fill components in dependent assemblies. foreach (var assemblyName in assembly.GetReferencedAssemblies()) { var dependency = Assembly.Load(assemblyName); ((IEnumerable <Type>)dependency.GetTypes()) .Where(t => t.CustomAttributes.Any(ca => typeof(ComponentAttribute).IsAssignableFrom(ca.AttributeType))) .ForEach(t => ReadyInstance(t, Container.Retrieve(t))); } }