Datei: NDOPackage/NDOProviderFactory.cs
Last Commit (33e9857)
1 | using NDOInterfaces; |
2 | using System; |
3 | using System.Collections.Generic; |
4 | using System.Diagnostics; |
5 | using System.IO; |
6 | using System.Linq; |
7 | using System.Reflection; |
8 | using System.Text; |
9 | using System.Threading.Tasks; |
10 | |
11 | namespace NDOVsPackage |
12 | { |
13 | ····class NDOProviderFactory |
14 | ····{ |
15 | ········private Dictionary<string, IProvider> ndoProviders; |
16 | ········private static object lockObject = new object(); |
17 | ········private NDOProviderFactory() |
18 | ········{ |
19 | ········} |
20 | |
21 | ········public static NDOProviderFactory Instance = new NDOProviderFactory(); |
22 | ········public IProvider this[string name] |
23 | ········{ |
24 | ············get |
25 | ············{ |
26 | ················if (this.ndoProviders == null) |
27 | ················{ |
28 | ····················lock (lockObject) |
29 | ····················{ |
30 | ························if (this.ndoProviders == null) // double check |
31 | ························{ |
32 | ····························this.ndoProviders = new Dictionary<string, IProvider>(); |
33 | ····························string path = Path.GetDirectoryName(GetType().Assembly.Location); |
34 | |
35 | ····························var ifc = typeof(IProvider); |
36 | |
37 | ····························foreach (var fileName in Directory.GetFiles(path, "*Provider.dll")) |
38 | ····························{ |
39 | ································try |
40 | ································{ |
41 | ····································var assembly = Assembly.LoadFrom(fileName); |
42 | ····································foreach (var type in assembly.ExportedTypes) |
43 | ····································{ |
44 | ········································if (ifc.IsAssignableFrom(type)) |
45 | ········································{ |
46 | ············································var p = (IProvider)Activator.CreateInstance(type); |
47 | ············································this.ndoProviders.Add(p.Name, p); |
48 | ········································} |
49 | ····································} |
50 | ································} |
51 | ································catch (Exception ex) |
52 | ································{ |
53 | ····································Debug.WriteLine(fileName + ": " + ex.ToString()); |
54 | ································} |
55 | ····························} |
56 | ························} |
57 | ····················} |
58 | ················} |
59 | |
60 | ················IProvider result = null; |
61 | ················ndoProviders.TryGetValue(name, out result); |
62 | ················return result; |
63 | ············} |
64 | ········} |
65 | ····} |
66 | } |
67 | ··} |
68 | ····} |
69 | } |
70 |
New Commit (aa458ff)
1 | // |
2 | // Copyright (c) 2002-2022 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using NDOInterfaces; |
24 | using System; |
25 | using System.Collections.Generic; |
26 | using System.Diagnostics; |
27 | using System.IO; |
28 | using System.Linq; |
29 | using System.Reflection; |
30 | using System.Text; |
31 | using System.Threading.Tasks; |
32 | |
33 | namespace NDOVsPackage |
34 | { |
35 | ····class NDOProviderFactory |
36 | ····{ |
37 | ········private Dictionary<string, IProvider> ndoProviders; |
38 | ········private static object lockObject = new object(); |
39 | ········private NDOProviderFactory() |
40 | ········{ |
41 | ········} |
42 | |
43 | ········public static NDOProviderFactory Instance = new NDOProviderFactory(); |
44 | ········public IProvider this[string name] |
45 | ········{ |
46 | ············get |
47 | ············{ |
48 | ················if (this.ndoProviders == null) |
49 | ················{ |
50 | ····················lock (lockObject) |
51 | ····················{ |
52 | ························if (this.ndoProviders == null) // double check |
53 | ························{ |
54 | ····························this.ndoProviders = new Dictionary<string, IProvider>(); |
55 | ····························string path = Path.GetDirectoryName(GetType().Assembly.Location); |
56 | |
57 | ····························var ifc = typeof(IProvider); |
58 | |
59 | ····························foreach (var fileName in Directory.GetFiles(path, "*Provider.dll")) |
60 | ····························{ |
61 | ································try |
62 | ································{ |
63 | ····································var assembly = Assembly.LoadFrom(fileName); |
64 | ····································foreach (var type in assembly.ExportedTypes) |
65 | ····································{ |
66 | ········································if (ifc.IsAssignableFrom(type)) |
67 | ········································{ |
68 | ············································var p = (IProvider)Activator.CreateInstance(type); |
69 | ············································this.ndoProviders.Add(p.Name, p); |
70 | ········································} |
71 | ····································} |
72 | ································} |
73 | ································catch (Exception ex) |
74 | ································{ |
75 | ····································Debug.WriteLine(fileName + ": " + ex.ToString()); |
76 | ································} |
77 | ····························} |
78 | ························} |
79 | ····················} |
80 | ················} |
81 | |
82 | ················IProvider result = null; |
83 | ················ndoProviders.TryGetValue(name, out result); |
84 | ················return result; |
85 | ············} |
86 | ········} |
87 | ····} |
88 | } |
89 |