Exemple #1
0
                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)));
                    }
                }
Exemple #2
0
        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");
        }
                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);
                }
Exemple #4
0
                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));
                }
                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));
                    }
                }