private static MethodInfo Get( Type type, string methodName, BindingFlags bindingFlags, Func <MethodInfo, bool> checkIfEquals = null, bool ifNotFoundThrowAnException = true) { XContract.ArgIsNotNull(type, nameof(type)); XContract.ArgIsNotNull(methodName, nameof(methodName)); try { var methodInfo = type.GetMethod(methodName, bindingFlags); if (methodInfo == null && ifNotFoundThrowAnException) { throw XArgumentException.Create(nameof(methodName), $"Method '{methodName}' not found on type {type.Name}."); } return(methodInfo); } catch (AmbiguousMatchException) { if (checkIfEquals == null) { throw; } return(type .GetMethods(bindingFlags) .FirstOrDefault(info => info.Name == methodName && checkIfEquals(info))); } }
public static void IsEnumValid <T>(string paramName, T value) where T : struct { var type = typeof(T); if (!type.IsEnum || !Enum.IsDefined(type, value)) { throw XArgumentException.CreateInvalidEnumValue(paramName, value as Enum); } }
public int CompareTo(object value) { if (value is TimeSpan span) { return(((TimeSpan)this).CompareTo(span)); } throw XArgumentException.Create(nameof(value), "Value must be a TimeSpan"); }
public static void ArgMustBe <TContract>(object argument, string argumentName) { if (argument is TContract) { return; } var exception = XArgumentException.CreateInvalidObjectType(argumentName, typeof(TContract)); Debug.Assert(true, exception.Message); throw exception; }
private static FieldInfo Get(Type type, string fieldName, BindingFlags bindingFlags, bool ifNotFoundThrowAnException = true) { XContract.ArgIsNotNull(fieldName, nameof(fieldName)); XContract.ArgIsNotNull(type, nameof(type)); var fieldInfo = type.GetField(fieldName, bindingFlags); if (fieldInfo == null && ifNotFoundThrowAnException) { throw XArgumentException.Create(nameof(fieldName), $"Field '{fieldName}' not found on type {type.Name}."); } return(fieldInfo); }
public static object InvokeGeneric(object obj, MethodInfo method, Type[] genericTypes, params object[] arguments) { XContract.ArgIsNotNull(obj, nameof(obj)); XContract.ArgIsNotNull(method, nameof(method)); XContract.ArgIsNotNull(genericTypes, nameof(genericTypes)); if (genericTypes.Length == 0) { throw XArgumentException.Create(nameof(genericTypes), "A quantidade de parâmetros genéricos não pode ser 0 (zero)."); } var generic = method.MakeGenericMethod(genericTypes); return(generic.Invoke(obj, arguments)); }
public SemanticVersion Create(Version version, string specialVersion, string orginalValue) { if (version == null) { throw XArgumentException.IsNull(nameof(version)); } var semanticVersion = new SemanticVersion { SpecialVersion = specialVersion ?? string.Empty, OrginalValue = string.IsNullOrEmpty(orginalValue) ? version + (!string.IsNullOrEmpty(specialVersion) ? '-' + specialVersion : null) : orginalValue, }; XNotImplemented.CommentedOutTemporarily("Need to revist the source documentation and complete the algorithm."); return(semanticVersion); }
private static PropertyInfo Get(Type type, string propertyName, BindingFlags bindingFlags, bool ifNotFoundThrowAnException = true) { XContract.ArgIsNotNull(type, nameof(type)); XContract.ArgIsNotNull(propertyName, nameof(propertyName)); try { var propertyInfo = type.GetProperty(propertyName, bindingFlags); if (propertyInfo == null && ifNotFoundThrowAnException) { throw XArgumentException.Create(nameof(propertyName), $"Property '{propertyName}' not found on type {type.Name}."); } return(propertyInfo); } catch (AmbiguousMatchException) { return(type .GetProperties(InstanceBindingFlags) .FirstOrDefault(info => info.Name == propertyName)); } }