我正在使用C#开发类似Nagios的系统监控应用程序。我定义了一个插件接口:
public interface IPlugin
{
PluginResult Execute();
}
每个插件根据其功能不同,都会有一个可变数量的参数。例如,ping插件可能需要主机名、数据包数量、超时值等参数。我希望用户能够在我的用户界面中为每个服务定义这些参数,但显然在应用程序发现可用的插件之前,这些参数是未知的。我很好奇其他人如何设计插件,使得应用程序可以发现这些可变参数。
例如,我现在有一个ping插件:
public class PingPlugin : IPlugin
{
private const string RESULT_MESSAGE = "Average ms: {0}; Packet loss: {1}";
private string _hostname;
private int _packets;
private int _timeout;
private int _warningTimeThreshold;
private int _warningLossThreshold;
private int _errorTimeThreshold;
private int _errorLossThreshold;
public PingPlugin(
string hostname,
int packets,
int timeout,
int warningTimeThreshold,
int warningLossThreshold,
int errorTimeThreshold,
int errorLossThreshold)
{
_hostname = hostname;
_packets = packets;
_timeout = timeout;
_warningTimeThreshold = warningTimeThreshold;
_warningLossThreshold = warningLossThreshold;
_errorTimeThreshold = errorTimeThreshold;
_errorLossThreshold = errorLossThreshold;
}
public PluginResult Execute()
{
// execute the plugin
}
}
我想通过反射来发现构造函数参数,并使用属性网格向用户呈现以允许配置插件,但我不确定在这种设计中提供一组默认值的最佳方法是什么。还有哪些替代方案?