private static object CustomDurationHandler(object sender, ParseErrorArgs args) { Regex regex = new Regex("[*]{2}(\\d+)Hrs(\\d+)Mins(\\d+)Secs[*]{2}"); if (args.FieldType == typeof(TimeSpan)) { Debug.Print("Object field : {0}, Invalid value : {1}", args.FieldName, args.InvalidValue); string duration = regex.Replace(args.InvalidValue, "PT$1H$2M$3S"); TimeSpan newValue = Duration.ParseTimeSpan(duration); Debug.Print("New value : {0}", newValue); return newValue; } // Here we handle only TimeSpan instances, so rethrow original exception with other types throw args.Exception; }
private static object CustomDurationHandler(object sender, ParseErrorArgs args) { Regex regex = new Regex("[*]{2}(\\d+)Hrs(\\d+)Mins(\\d+)Secs[*]{2}"); if (args.FieldType == typeof(TimeSpan)) { Debug.Print("Object field : {0}, Invalid value : {1}", args.FieldName, args.InvalidValue); string duration = regex.Replace(args.InvalidValue, "PT$1H$2M$3S"); TimeSpan newValue = Duration.ParseTimeSpan(duration); Debug.Print("New value : {0}", newValue); return(newValue); } // Here we handle only TimeSpan instances, so rethrow original exception with other types throw args.Exception; }
private static object CustomDurationHandlerForFile(object sender, ParseErrorArgs args) { var regex = new Regex("[*]{2}(\\d+)Hrs(\\d+)Mins(\\d+)Secs[*]{2}"); if (args.FieldType != typeof(TimeSpan)) { throw args.Exception; } Console.WriteLine("Object field: {0}, Object field type: {1}, Invalid value: {2}", args.FieldName, args.FieldType, args.InvalidValue); var duration = regex.Replace(args.InvalidValue, "PT$1H$2M$3S"); var value = Duration.ParseTimeSpan(duration); Console.WriteLine("New value : {0}", value); return(value); }