markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / GenericPluginLoader.cs @ e024b149
이력 | 보기 | 이력해설 | 다운로드 (3.5 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($"Plugin Not Found. {path}"); |
42 |
} |
43 |
|
44 |
foreach(string dllFile in dllFileNames) |
45 |
{ |
46 |
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 |
|
61 |
} |
62 |
} |
63 |
catch (Exception ex) |
64 |
{ |
65 |
throw new Exception($"{dllFile} Get InterFaces Error",ex); |
66 |
} |
67 |
} |
68 |
|
69 |
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 |
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 |
} |
105 |
|
106 |
return plugins; |
107 |
} |
108 |
|
109 |
return null; |
110 |
} |
111 |
} |
112 |
} |