コード例 #1
0
        public static IEnumerable <Type> GetPluginSources <T>(string unitPackagesName = "UnitPackages", string searchPackagePattern = "*.dll") where T : class
        {
            List <Type> moduleTypes = new List <Type>();

            if (string.IsNullOrEmpty(unitPackagesName))
            {
                throw new ArgumentException("UnitPackagesName parameter cannot be empty");
            }

            if (string.IsNullOrEmpty(searchPackagePattern))
            {
                throw new ArgumentException("SearchPackagePattern parameter cannot be empty");
            }

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, unitPackagesName);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            DirectoryInfo root = new DirectoryInfo(path);

            DirectoryInfo[] dics = root.GetDirectories();

            foreach (var item in dics)
            {
                var files = item.GetFiles(searchPackagePattern).ToList();

                foreach (var file in files)
                {
                    using (var fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
                    {
                        var context = new PluginLoadContext(file.FullName);

                        var assembly = context.LoadFromStream(fs);

                        context.SetBaseAssembly(assembly);

                        var types = assembly.GetTypes().Where(type =>
                                                              type.IsClass &&
                                                              !type.IsAbstract &&
                                                              !type.IsGenericType &&
                                                              typeof(T).IsAssignableFrom(type)).ToList();

                        if (types.Count > 0)
                        {
                            moduleTypes.AddRange(types);

                            context.ResgiterContext();
                        }
                    }
                }
            }

            return(moduleTypes);
        }
コード例 #2
0
        public static IEnumerable <CompiledRazorAssemblyPart> GetPluginRazors(string unitPackagesName = "UnitPackages", string searchPackagePattern = "*.Views.dll")
        {
            List <CompiledRazorAssemblyPart> compiledRazorAssemblyParts = new List <CompiledRazorAssemblyPart>();

            if (string.IsNullOrEmpty(unitPackagesName))
            {
                throw new ArgumentException("UnitPackagesName parameter cannot be empty");
            }

            if (string.IsNullOrEmpty(searchPackagePattern))
            {
                throw new ArgumentException("SearchPackagePattern parameter cannot be empty");
            }

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, unitPackagesName);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            DirectoryInfo root = new DirectoryInfo(path);

            DirectoryInfo[] dics = root.GetDirectories();

            foreach (var item in dics)
            {
                var files = item.GetFiles(searchPackagePattern).ToList();

                foreach (var file in files)
                {
                    using (var fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
                    {
                        var context = new PluginLoadContext(file.FullName);

                        var assembly = context.LoadFromStream(fs);

                        var viewAssemblyPart = new CompiledRazorAssemblyPart(assembly);

                        compiledRazorAssemblyParts.Add(viewAssemblyPart);
                    }
                }
            }

            return(compiledRazorAssemblyParts);
        }