Datei: NDOPackage/NDOProviderFactory.cs
Last Commit (946ad0e)
| 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 NETDataObjects. 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 |
New 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 |