1

How to access non-public members for NCrontab ?

string time_A = "20,25,30 20 * * 1-5";
var schedule = NCrontab.CrontabSchedule.Parse(time_A);

I can see Non-Public members of 'schedule' from the debug,

but I want to get value of _day and _hours.

  • _days {*} NCrontab.CrontabField
  • _daysOfWeek {1-5} NCrontab.CrontabField
  • _hours {20} NCrontab.CrontabField
  • _minutes {20,25,30} NCrontab.CrontabField
  • _months {*} NCrontab.CrontabField _seconds null NCrontab.CrontabField

From I need to access a non-public member (Highlighted Item) of a Combo Box, seems like it does not work for the following line.

PropertyInfo highlightedItemProperty = schedule.GetType().GetProperties(BindingFlags.NonPublic  | BindingFlags.Instance).Single(pi => pi.Name == "_days");

Please advise.

Updated !!!

I can fix it now.

string time_A = "20,25,30 18-22 * * 1-5";
var schedule = CrontabSchedule.Parse(time_A);
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var field = schedule.GetType().GetField("_hours", bindingFlags);
var getfield = field.GetValue(schedule);
  • You can use `typeof(Schedule).GetField(BindingFlags.NonPublic|BindingFlags.Instance)`, fieldName)'. But realize that it is not a good idea to rely on implementation details like this. – jeroenh Aug 16 '18 at 12:19
  • Thank you for providing me the link !!! It can helps me. – Snowprincez Coffee Aug 17 '18 at 05:13

0 Answers0