Esempio n. 1
0
 private static void AttemptLocateInitializer(Type mainClass, ref ModuleInfoInternal infos)
 {
     foreach (var m in mainClass.GetMethods())
     {
         if (m.IsStatic && m.GetCustomAttribute <Shared.Attributes.ModuleInitializer>() != null)
         {
             infos.Initializer = m;
             return;
         }
     }
 }
Esempio n. 2
0
        public static ModuleInfoInternal InjectModule(IServiceCollection services, IConfiguration cfg, IMvcBuilder builder, string path)
        {
            FileInfo           inf  = new FileInfo(Program.AssemblyDirectory + "/" + path + ".dll");
            ModuleInfoInternal info = new ModuleInfoInternal()
            {
                Name = path
            };
            Type   cfgClass    = null;
            Type   moduleClass = null;
            object config      = null;

            if (!inf.Exists)
            {
                return(null);
            }
            Assembly asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(inf.FullName);

            info.Version = asm.GetName().Version.ToString();
            foreach (Type t in asm.GetExportedTypes())
            {
                if (t.GetCustomAttribute <Shared.Attributes.Module>() != null)
                {
                    if (moduleClass != null)
                    {
                        throw new ArgumentException("Duplicate Module Main class");
                    }
                    info.ModuleInterface = t.GetCustomAttribute <Shared.Attributes.Module>().Interface;
                    moduleClass          = t;
                }
                else if (t.GetCustomAttribute <Shared.Attributes.Configurator>() != null)
                {
                    if (cfgClass != null)
                    {
                        throw new ArgumentException("Duplicate Config class");
                    }
                    cfgClass = t;
                }
            }
            if (!info.ModuleInterface.IsAssignableFrom(moduleClass))
            {
                throw new ArgumentException("Cannot bind module : incorrect base class");
            }
            builder.AddApplicationPart(asm);
            if (cfgClass != null)
            {
                config = InjectModuleConfs(services, info.Name, cfgClass, cfg);
            }
            AttemptLocateInitializer(moduleClass, ref info);
            info.ModuleInterface = InjectService(services, config, moduleClass, info.ModuleInterface);
            return(info);
        }
Esempio n. 3
0
        public static void AttemptCallModuleInitializer(IHostingEnvironment host, ModuleInfoInternal info, object moduleClass)
        {
            if (info.Initializer != null)
            {
                var attribute = info.Initializer.GetCustomAttribute <Shared.Attributes.ModuleInitializer>();

                if (host.IsDevelopment() && !attribute.Debug)
                {
                    return;
                }
                else if (!attribute.Release && !host.IsDevelopment())
                {
                    return;
                }
                info.Initializer.Invoke(null, new object[] { moduleClass });
            }
        }