private IParser <T> ApplyOptionsAndGetReferenceTypeParserObject <T>(ParseFunc <T> parseFunc, TryParseFunc <T> tryParseFunc, MissingTryParseHandling missingTryParse) where T : class { if (parseFunc is null) { return(new FunctorParserObject <T>(ParseThrowsNotSupportedException <T>, TryParseThrowsNotSupportedException)); } ParseFunc <T> referenceTypeParseFunc = parseFunc; TryParseFunc <T> referenceTypeTryParseFunc = tryParseFunc; if (_referenceTypesParseNullToNull) { referenceTypeParseFunc = s => { if (s is null) { return(null); } return(parseFunc(s)); }; } if (tryParseFunc is null) { if (missingTryParse == MissingTryParseHandling.WrapParseInTryCatch) { referenceTypeTryParseFunc = CreateWrapperForTryParse(referenceTypeParseFunc); } else if (missingTryParse == MissingTryParseHandling.ReturnFalse) { referenceTypeTryParseFunc = TryParseReturnsFalse; } else { referenceTypeTryParseFunc = TryParseThrowsNotSupportedException; } } else { if (_referenceTypesParseNullToNull) { referenceTypeTryParseFunc = (string s, out T result) => { if (s is null) { result = null; return(true); } return(tryParseFunc(s, out result)); }; } } return(new FunctorParserObject <T>(referenceTypeParseFunc, referenceTypeTryParseFunc)); }
private static TryParseFunc <T> CreateTryParseFuncForTypeConverter <T>(TypeConverter converter, MissingTryParseHandling missingTryParse) => null;
private IParser <T> ApplyOptionsAndGetValueTypeParserObject <T>(ParseFunc <T> parseFunc, TryParseFunc <T> tryParseFunc, MissingTryParseHandling missingTryParse) where T : struct { if (parseFunc is null) { return(new FunctorParserObject <T>(ParseThrowsNotSupportedException <T>, TryParseThrowsNotSupportedException)); } ParseFunc <T> valueTypeParseFunc = parseFunc; // Just keeping with the pattern. TryParseFunc <T> valueTypeTryParseFunc = tryParseFunc; if (tryParseFunc is null) { if (missingTryParse == MissingTryParseHandling.WrapParseInTryCatch) { valueTypeTryParseFunc = CreateWrapperForTryParse(valueTypeParseFunc); } else if (missingTryParse == MissingTryParseHandling.ReturnFalse) { valueTypeTryParseFunc = TryParseReturnsFalse; } else { valueTypeTryParseFunc = TryParseThrowsNotSupportedException; } } return(new FunctorParserObject <T>(valueTypeParseFunc, valueTypeTryParseFunc)); }
private IParser <T> AltCreateParserObject <T>(ParseFunc <T> parseFunc, TryParseFunc <T> tryParseFunc, MissingTryParseHandling missingTryParse) { Type type = typeof(T); string methodName = type.GetTypeInfo().IsValueType ? nameof(ApplyOptionsAndGetValueTypeParserObject) : nameof(ApplyOptionsAndGetReferenceTypeParserObject); MethodInfo factoryMethod = ReflectionHelper.GetPrivateGenericMethod(typeof(ParserContainer), methodName, type); Func <ParseFunc <T>, TryParseFunc <T>, MissingTryParseHandling, IParser <T> > factory = factoryMethod.CreateDelegate <Func <ParseFunc <T>, TryParseFunc <T>, MissingTryParseHandling, IParser <T> > >(this); return(factory(parseFunc, tryParseFunc, missingTryParse)); }
/// <summary> /// Configures parsing of <typeparamref name="T"/> to use its type converter. /// </summary> /// <typeparam name="T">The type for which to configure parsing.</typeparam> /// <param name="missingTryParse">A value indicating how to configure TryParse functionality if the type does not have a TryParse method</param> public abstract void UseTypeConverter <T>(MissingTryParseHandling missingTryParse);
/// <summary> /// Configures parsing of <typeparamref name="T"/> to use the provided function. /// </summary> /// <typeparam name="T">The type for which to configure parsing.</typeparam> /// <param name="parseFunc">A function that implements Parse method functionality for <typeparamref name="T"/>.</param> /// <param name="missingTryParse">A value indicating how to configure TryParse functionality if the type does not have a TryParse method</param> public abstract void UseFunc <T>(ParseFunc <T> parseFunc, MissingTryParseHandling missingTryParse);
/// <summary> /// Configures parsing of <typeparamref name="T"/> to explicitly use the default implementation. /// </summary> /// <typeparam name="T">The type for which to configure parsing.</typeparam> /// <param name="missingTryParse">A value indicating how to configure TryParse functionality if the type does not have a TryParse method</param> public abstract void UseDefault <T>(MissingTryParseHandling missingTryParse);