我从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);
所以,现在我需要用prooper参数调用这个方法。
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 });
非常感谢你的帮助。
答案 0 :(得分:1)
MethodInfo generic = addOrUpdate.MakeGenericMethod(scenarioType);
ParameterExpression param = Expression.Parameter(scenarioType, "x");
ConstantExpression someValue = Expression.Constant(jobName, typeof(string));
MethodCallExpression methodCall = Expression.Call(param, scenarioType.GetMethod("Execute", new Type[] { typeof(string) }), someValue);
LambdaExpression expre = Expression.Lambda(methodCall, new ParameterExpression[] { param });
generic.Invoke(self, new object[] { jobName, expre, cron, TimeZoneInfo.Utc, null });
它有效:|