【发布时间】:2016-09-15 07:57:58
【问题描述】:
我像这样从 RecurringJob 调用 AddOrUpdate 方法
public override string StartWork()
{
RecurringJob.AddOrUpdate<SomeScenario>(jobEntity.Name, x => x.Execute(jobEntity.Name), cron, TimeZoneInfo.Utc);
}
我需要重写这个方法,来反射调用。 我找到了正确的方法重载
MethodInfo addOrUpdate = typeof(RecurringJob).GetMethods().Where(x => x.Name == "AddOrUpdate" && x.IsGenericMethod && x.IsGenericMethodDefinition).Select(m => new
{
Method = m,
Params = m.GetParameters(),
Args = m.GetGenericArguments()
})
.Where(x => x.Params.Length == 5
&& x.Params[0].ParameterType == typeof(string)
&& x.Params[2].ParameterType == typeof(string)
&& x.Params[3].ParameterType == typeof(TimeZoneInfo)
&& x.Params[4].ParameterType == typeof(string)
)
.Select(x => x.Method).FirstOrDefault();
我在 db 中持有正确的类型,所以我会这样得到它
Type type = Type.GetType(jobEntity.ScenarioType);
MethodInfo generic = addOrUpdate.MakeGenericMethod(type);
所以,现在我需要使用适当的参数调用此方法。
public static void AddOrUpdate<T>(string recurringJobId, Expression<Action<T>> methodCall, string cronExpression, TimeZoneInfo timeZone = null, string queue = "default")
问题:在这种情况下我不知道如何生成Expression<Action<T>>,以调用generic.Invoke(this, new object[] { jobEntity.Name, Expression<Action<T>>, cron, TimeZoneInfo.Utc, null });
非常感谢您的帮助。
【问题讨论】:
标签: c# .net generics reflection hangfire