markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / GenericPluginLoader.cs @ ab7fe8c0
이력 | 보기 | 이력해설 | 다운로드 (3.5 KB)
1 | f363a40e | taeseongkim | /* |
---|---|---|---|
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 | 43e1d368 | taeseongkim | using System.Linq; |
22 | f363a40e | taeseongkim | |
23 | db0d3db3 | taeseongkim | namespace Markus.Service.Extensions |
24 | f363a40e | taeseongkim | { |
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 | 43e1d368 | taeseongkim | { |
33 | ICollection<Assembly> assemblies = new List<Assembly>(); |
||
34 | ICollection<T> plugins = new List<T>(); |
||
35 | Type pluginType = typeof(T); |
||
36 | |||
37 | 950e6b84 | taeseongkim | dllFileNames = Directory.GetFiles(path, "*.dll",SearchOption.AllDirectories); |
38 | f363a40e | taeseongkim | |
39 | 43e1d368 | taeseongkim | if(dllFileNames.Count() == 0) |
40 | { |
||
41 | a5e5fff6 | taeseongkim | throw new Exception($"Plugin Not Found. {path}"); |
42 | 43e1d368 | taeseongkim | } |
43 | |||
44 | f363a40e | taeseongkim | foreach(string dllFile in dllFileNames) |
45 | { |
||
46 | a5e5fff6 | taeseongkim | try |
47 | { |
||
48 | AssemblyName an = AssemblyName.GetAssemblyName(dllFile); |
||
49 | Assembly assembly = Assembly.Load(an); |
||
50 | |||
51 | if (assembly.GetTypes()?.Count() > 0) |
||
52 | { |
||
53 | if (assembly.GetTypes()[0].GetInterfaces().Count(x => x == pluginType) > 0) |
||
54 | { |
||
55 | assemblies.Add(assembly); |
||
56 | } |
||
57 | } |
||
58 | else |
||
59 | { |
||
60 | f363a40e | taeseongkim | |
61 | a5e5fff6 | taeseongkim | } |
62 | } |
||
63 | catch (Exception ex) |
||
64 | { |
||
65 | throw new Exception($"{dllFile} Get InterFaces Error",ex); |
||
66 | } |
||
67 | } |
||
68 | f363a40e | taeseongkim | |
69 | 43e1d368 | taeseongkim | ICollection<Type> pluginTypes = new List<Type>(); |
70 | foreach (Assembly assembly in assemblies) |
||
71 | { |
||
72 | if (assembly != null) |
||
73 | { |
||
74 | Type[] types = assembly.GetTypes(); |
||
75 | |||
76 | foreach (Type type in types) |
||
77 | { |
||
78 | if (type.IsInterface || type.IsAbstract) |
||
79 | { |
||
80 | continue; |
||
81 | } |
||
82 | else |
||
83 | { |
||
84 | if (type.GetInterface(pluginType.FullName) != null) |
||
85 | { |
||
86 | pluginTypes.Add(type); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | foreach (Type type in pluginTypes) |
||
94 | { |
||
95 | a5e5fff6 | taeseongkim | try |
96 | { |
||
97 | T plugin = (T)Activator.CreateInstance(type); |
||
98 | plugins.Add(plugin); |
||
99 | } |
||
100 | catch (Exception ex) |
||
101 | { |
||
102 | throw new Exception($"{type.Name} Get InterFaces Error", ex); |
||
103 | } |
||
104 | 43e1d368 | taeseongkim | } |
105 | f363a40e | taeseongkim | |
106 | 43e1d368 | taeseongkim | return plugins; |
107 | f363a40e | taeseongkim | } |
108 | |||
109 | return null; |
||
110 | } |
||
111 | } |
||
112 | } |