-
Notifications
You must be signed in to change notification settings - Fork 200
/
GenericPluginConfigurator.cs
128 lines (115 loc) · 4.54 KB
/
GenericPluginConfigurator.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
namespace dotless.Core.Plugins
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;
public class GenericPluginConfigurator<T> : IPluginConfigurator where T : IPlugin
{
public string Name
{
get
{
return PluginFinder.GetName(typeof(T));
}
}
public string Description
{
get
{
return PluginFinder.GetDescription(typeof(T));
}
}
public Type Configurates
{
get
{
return typeof(T);
}
}
private Func<IPlugin> _pluginCreator = null;
public void SetParameterValues(IEnumerable<IPluginParameter> pluginParameters)
{
ConstructorInfo defaultConstructor;
ConstructorInfo parameterConstructor;
GetConstructorInfos(out parameterConstructor, out defaultConstructor);
if (pluginParameters == null || pluginParameters.Count() == 0 || pluginParameters.Any(parameter => parameter.Value == null))
{
if (defaultConstructor == null)
{
throw new Exception("No parameters provided but no default constructor");
}
_pluginCreator = () => (T)defaultConstructor.Invoke(new object[] { });
}
else
{
var constructorArguments = parameterConstructor.GetParameters()
.OrderBy(parameter => parameter.Position)
.Select(parameter => pluginParameters.First(pluginParameter => pluginParameter.Name == parameter.Name).Value)
.ToArray();
_pluginCreator = () => (T)parameterConstructor.Invoke(constructorArguments);
}
}
public IPlugin CreatePlugin()
{
if (_pluginCreator == null)
{
SetParameterValues(null);
}
return _pluginCreator();
}
private class ConstructorParameterSet
{
public ParameterInfo[] Parameter { get; set; }
public int Count { get; set; }
}
private void GetConstructorInfos(out ConstructorInfo parameterConstructor, out ConstructorInfo defaultConstructor)
{
List<ConstructorInfo> constructors = typeof(T).GetConstructors()
.Where(constructorInfo => constructorInfo.IsPublic && !constructorInfo.IsStatic).ToList();
if (constructors.Count > 2 || constructors.Count == 0)
{
throw new Exception("Generic plugin configurator doesn't support less than 1 or more than 2 constructors. Add your own IPluginConfigurator to the assembly.");
} else if (constructors.Count == 2)
{
if (constructors[0].GetParameters().Length == 0)
{
defaultConstructor = constructors[0];
parameterConstructor = constructors[1];
} else if (constructors[1].GetParameters().Length == 0)
{
defaultConstructor = constructors[1];
parameterConstructor = constructors[0];
} else
{
throw new Exception("Generic plugin configurator only supports 1 parameterless constructor and 1 with parameters. Add your own IPluginConfigurator to the assembly.");
}
} else {
if (constructors[0].GetParameters().Length == 0)
{
defaultConstructor = constructors[0];
parameterConstructor = null;
}
else
{
defaultConstructor = null;
parameterConstructor = constructors[0];
}
}
}
public IEnumerable<IPluginParameter> GetParameters()
{
ConstructorInfo defaultConstructor;
ConstructorInfo parameterConstructor;
GetConstructorInfos(out parameterConstructor, out defaultConstructor);
if (parameterConstructor == null)
{
return new List<IPluginParameter>();
}
return parameterConstructor.GetParameters().Select(parameter => (IPluginParameter)new PluginParameter(
parameter.Name, parameter.ParameterType, defaultConstructor == null)).ToList();
}
}
}