Esempio n. 1
0
        /// <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)));
            }
        }
Esempio n. 2
0
        /// <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);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Runs a Snow app with optional parameters.
 /// </summary>
 /// <param name="runnable">Snow runnable class</param>
 /// <param name="args">Parameters</param>
 public static void Run(ISnowRunnable runnable, string[]?args = null)
 {
     SnowReflection.InstanceComponents(runnable);
     SnowReflection.InjectDependencies(runnable);
     runnable.Run(args);
 }