【发布时间】:2020-11-02 13:39:12
【问题描述】:
考虑通过IHealthChecksBuilder 添加多项健康检查。
循环和使用相同类型的类的正确语法是什么?
以下结果
c 是一种类型,但用作变量。
如何为委托定义类?
var checks = new List<Tuple<Type, string, HealthStatus, string[]>>()
{
new Tuple<Type, string, HealthStatus, string[]>(typeof(CustomHealthCheckService), "some_health_check", HealthStatus.Unhealthy, new[] {"tag"}),
new Tuple<Type, string, HealthStatus, string[]>(typeof(OtherCustomHealthCheckService), "some_other_health_check", HealthStatus.Unhealthy, new[] {"other_tag"})
};
IHealthChecksBuilder builder = services.AddHealthChecks();
foreach (var c in checks)
{
builder.AddCheck<c.Item1>(c.Item2, c.Item3, c.Item4);
}
【问题讨论】:
-
为什么不使用
List<Action<IHealthChecksBuilder>>并用builder => builder.AddCheck<CustomHealthCheckService>("some_health_check", HealthStatus.Unhealthy, new[] {"tag"})之类的东西填充它,那么它就是foreach(var c in checks) c(builder); -
Builder 应该有接受 Type 的非泛型版本。如果没有,那么您就不走运了(仍然可以使用反射,但最好不要使用)。或者使用上面的建议。