프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / GenericPluginLoader.cs @ 43e1d368

이력 | 보기 | 이력해설 | 다운로드 (2.72 KB)

1
/*
2
   Copyright 2013 Christoph Gattnar
3

    
4
   Licensed under the Apache License, Version 2.0 (the "License");
5
   you may not use this file except in compliance with the License.
6
   You may obtain a copy of the License at
7

    
8
	   http://www.apache.org/licenses/LICENSE-2.0
9

    
10
   Unless required by applicable law or agreed to in writing, software
11
   distributed under the License is distributed on an "AS IS" BASIS,
12
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
   See the License for the specific language governing permissions and
14
   limitations under the License.
15
*/
16

    
17
using System;
18
using System.Collections.Generic;
19
using System.IO;
20
using System.Reflection;
21
using System.Linq;
22

    
23
namespace Markus.Service.Extensions
24
{
25
	public static class GenericPluginLoader<T>
26
	{
27
		public static ICollection<T> LoadPlugins(string path)
28
		{
29
			string[] dllFileNames = null;
30

    
31
			if(Directory.Exists(path))
32
            {
33
                ICollection<Assembly> assemblies = new List<Assembly>();
34
                ICollection<T> plugins = new List<T>();
35
				Type pluginType = typeof(T);
36

    
37
				dllFileNames = Directory.GetFiles(path, "*.dll",SearchOption.AllDirectories);
38

    
39
                if(dllFileNames.Count() == 0)
40
                {
41
                    throw new Exception($"download Plugin Not Found. {path}");
42
                }
43

    
44
				foreach(string dllFile in dllFileNames)
45
				{
46
					AssemblyName an = AssemblyName.GetAssemblyName(dllFile);
47
					Assembly assembly = Assembly.Load(an);
48

    
49
					if (assembly.GetTypes()[0].GetInterfaces().Count(x=>x == pluginType) > 0)
50
					{
51
                        assemblies.Add(assembly);
52
					}
53
				}
54

    
55
                ICollection<Type> pluginTypes = new List<Type>();
56
                foreach (Assembly assembly in assemblies)
57
                {
58
                    if (assembly != null)
59
                    {
60
                        Type[] types = assembly.GetTypes();
61

    
62
                        foreach (Type type in types)
63
                        {
64
                            if (type.IsInterface || type.IsAbstract)
65
                            {
66
                                continue;
67
                            }
68
                            else
69
                            {
70
                                if (type.GetInterface(pluginType.FullName) != null)
71
                                {
72
                                    pluginTypes.Add(type);
73
                                }
74
                            }
75
                        }
76
                    }
77
                }
78

    
79
                foreach (Type type in pluginTypes)
80
                {
81
                    T plugin = (T)Activator.CreateInstance(type);
82
                    plugins.Add(plugin);
83
                }
84

    
85
                return plugins;
86
			}
87

    
88
			return null;
89
		}
90
	}
91
}
클립보드 이미지 추가 (최대 크기: 500 MB)