jeudi 15 septembre 2016

hangfire, RecurringJob AddOrUpdate reflection call c#

I call AddOrUpdate method from RecurringJob like that

public override string StartWork()
{
        RecurringJob.AddOrUpdate<SomeScenario>(jobEntity.Name, x => x.Execute(jobEntity.Name), cron, TimeZoneInfo.Utc);
}

I need to rewrite this method, to reflection call. I found proper method overload

  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();

I Hold proper type in db, so I will get it like that

Type type = Type.GetType(jobEntity.ScenarioType);
MethodInfo generic = addOrUpdate.MakeGenericMethod(type);

So, now I need to invoke this method with prooper parameters.

public static void AddOrUpdate<T>(string recurringJobId, Expression<Action<T>> methodCall, string cronExpression, TimeZoneInfo timeZone = null, string queue = "default")

Problem: I don't know how to generate Expression<Action<T>> in this case, to invoke generic.Invoke(this, new object[] { jobEntity.Name, Expression<Action<T>>, cron, TimeZoneInfo.Utc, null });

Thank you a lot, for any help.





Aucun commentaire:

Enregistrer un commentaire