markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / GenericPluginLoader.cs @ 43e1d368
이력 | 보기 | 이력해설 | 다운로드 (2.72 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 | throw new Exception($"download Plugin Not Found. {path}"); |
||
42 | } |
||
43 | |||
44 | f363a40e | taeseongkim | foreach(string dllFile in dllFileNames) |
45 | { |
||
46 | AssemblyName an = AssemblyName.GetAssemblyName(dllFile); |
||
47 | Assembly assembly = Assembly.Load(an); |
||
48 | |||
49 | 43e1d368 | taeseongkim | if (assembly.GetTypes()[0].GetInterfaces().Count(x=>x == pluginType) > 0) |
50 | f363a40e | taeseongkim | { |
51 | 43e1d368 | taeseongkim | assemblies.Add(assembly); |
52 | f363a40e | taeseongkim | } |
53 | } |
||
54 | |||
55 | 43e1d368 | taeseongkim | 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 | f363a40e | taeseongkim | |
85 | 43e1d368 | taeseongkim | return plugins; |
86 | f363a40e | taeseongkim | } |
87 | |||
88 | return null; |
||
89 | } |
||
90 | } |
||
91 | } |