예제 #1
0
        public static bool TryCreateModule(string modulePath, out ModuleInfo moduleInfo)
        {
            string root = Path.GetFullPath(Path.Combine(
                                               Path.GetDirectoryName(
                                                   Path.GetDirectoryName(
                                                       Path.GetDirectoryName(
                                                           Path.GetDirectoryName(
                                                               Path.GetDirectoryName(typeof(Program).Assembly.Location)))))));

            string pluginLocation = Path.GetFullPath(Path.Combine(root, modulePath.Replace('\\', Path.DirectorySeparatorChar)));

            var context  = new PluginLoadContext(pluginLocation);
            var assembly = context.LoadFromAssemblyName(new AssemblyName(Path.GetFileNameWithoutExtension(pluginLocation)));

            foreach (Type type in assembly.GetTypes())
            {
                if (typeof(IModule).IsAssignableFrom(type))
                {
                    if (Activator.CreateInstance(type) is IModule module)
                    {
                        moduleInfo = new ModuleInfo(module, assembly, context);
                        return(true);
                    }
                }
            }

            // The .dll does not contain a class implementing IModule so it must not be one of our modules.
            moduleInfo = null;
            return(false);
        }
예제 #2
0
 private ModuleInfo(IModule module, Assembly assembly, PluginLoadContext context)
 {
     _module  = module;
     Assembly = assembly;
     _context = context;
 }