markus / ConvertService / ServiceBase / Markus.Service.Extensions / Exntensions / GenericPluginLoader.cs @ db0d3db3
이력 | 보기 | 이력해설 | 다운로드 (1.92 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 |
|
22 |
namespace Markus.Service.Extensions |
23 |
{ |
24 |
public static class GenericPluginLoader<T> |
25 |
{ |
26 |
public static ICollection<T> LoadPlugins(string path) |
27 |
{ |
28 |
string[] dllFileNames = null; |
29 |
|
30 |
if(Directory.Exists(path)) |
31 |
{ |
32 |
dllFileNames = Directory.GetFiles(path, "*.dll"); |
33 |
|
34 |
ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length); |
35 |
foreach(string dllFile in dllFileNames) |
36 |
{ |
37 |
AssemblyName an = AssemblyName.GetAssemblyName(dllFile); |
38 |
Assembly assembly = Assembly.Load(an); |
39 |
assemblies.Add(assembly); |
40 |
} |
41 |
|
42 |
Type pluginType = typeof(T); |
43 |
ICollection<Type> pluginTypes = new List<Type>(); |
44 |
foreach(Assembly assembly in assemblies) |
45 |
{ |
46 |
if(assembly != null) |
47 |
{ |
48 |
Type[] types = assembly.GetTypes(); |
49 |
|
50 |
foreach(Type type in types) |
51 |
{ |
52 |
if(type.IsInterface || type.IsAbstract) |
53 |
{ |
54 |
continue; |
55 |
} |
56 |
else |
57 |
{ |
58 |
if(type.GetInterface(pluginType.FullName) != null) |
59 |
{ |
60 |
pluginTypes.Add(type); |
61 |
} |
62 |
} |
63 |
} |
64 |
} |
65 |
} |
66 |
|
67 |
ICollection<T> plugins = new List<T>(pluginTypes.Count); |
68 |
foreach(Type type in pluginTypes) |
69 |
{ |
70 |
T plugin = (T)Activator.CreateInstance(type); |
71 |
plugins.Add(plugin); |
72 |
} |
73 |
|
74 |
return plugins; |
75 |
} |
76 |
|
77 |
return null; |
78 |
} |
79 |
} |
80 |
} |