-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathPingCastleFactory.cs
91 lines (85 loc) · 3.08 KB
/
PingCastleFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using PingCastle.Data;
using PingCastle.Exports;
using PingCastle.Healthcheck;
using PingCastle.Report;
using PingCastle.Scanners;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
namespace PingCastle
{
public class PingCastleFactory
{
public static Dictionary<string, Type> GetAllScanners()
{
var output = new Dictionary<string, Type>();
foreach (Type type in Assembly.GetAssembly(typeof(PingCastleFactory)).GetExportedTypes())
{
if (!type.IsAbstract && typeof(IScanner).IsAssignableFrom(type))
{
PropertyInfo pi = type.GetProperty("Name");
try
{
IScanner scanner = (IScanner)Activator.CreateInstance(type);
output.Add(scanner.Name, type);
}
catch (Exception ex)
{
Console.WriteLine("Unable to load the class " + type + " (" + ex.Message + ")");
}
}
}
return output;
}
public static IScanner LoadScanner(Type scannerType)
{
return (IScanner)Activator.CreateInstance(scannerType);
}
public static Dictionary<string, Type> GetAllExport()
{
var output = new Dictionary<string, Type>();
foreach (Type type in Assembly.GetAssembly(typeof(PingCastleFactory)).GetExportedTypes())
{
if (!type.IsAbstract && typeof(IExport).IsAssignableFrom(type))
{
PropertyInfo pi = type.GetProperty("Name");
IExport export = (IExport)Activator.CreateInstance(type);
output.Add(export.Name, type);
}
}
return output;
}
public static IExport LoadExport(Type scannerType)
{
return (IExport)Activator.CreateInstance(scannerType);
}
public static string GetFilePatternForLoad<T>() where T : IPingCastleReport
{
if (typeof(T) == typeof(HealthcheckData))
{
return "*ad_hc_*.xml";
}
throw new NotImplementedException("No file pattern known for type " + typeof(T));
}
static T GetImplementation<T>()
{
foreach (Type type in Assembly.GetAssembly(typeof(PingCastleFactory)).GetExportedTypes())
{
if (typeof(T).IsAssignableFrom(type) && !type.IsAbstract)
{
try
{
return (T)Activator.CreateInstance(type);
}
catch (Exception)
{
Trace.WriteLine("Unable to instanciate the type " + type);
throw;
}
}
}
throw new NotImplementedException("No implementation found for type " + typeof(T).ToString());
}
}
}