Example #1
0
                public static object Invoke(object obj, MethodInfo method, params object[] arguments)
                {
                    XContract.ArgIsNotNull(obj, nameof(obj));
                    XContract.ArgIsNotNull(method, nameof(method));

                    return(method.Invoke(obj, arguments));
                }
Example #2
0
                public static MethodInfo Get(object obj, string methodName, bool ifNotFoundThrowAnException = true)
                {
                    XContract.ArgIsNotNull(obj, nameof(obj));
                    XContract.ArgIsNotNull(methodName, nameof(methodName));

                    return(Get(obj.GetType(), methodName, InstanceBindingFlags, ifNotFoundThrowAnException: ifNotFoundThrowAnException));
                }
Example #3
0
                public static object InvokeStatic(Type objectType, string methodName, Type[] genericTypes, params object[] arguments)
                {
                    XContract.ArgIsNotNull(objectType, nameof(objectType));
                    XContract.ArgIsNotNull(methodName, nameof(methodName));

                    var generics = objectType
                                   .GetMethods(BindingFlags.Public | BindingFlags.Static)
                                   .Where(info => info.Name == methodName &&
                                          info.IsGenericMethod &&
                                          info.GetGenericArguments().Length == genericTypes.Length)
                                   .Select(
                        info =>
                    {
                        try
                        {
                            return(info.MakeGenericMethod(genericTypes));
                        }
                        catch (Exception)
                        {
                            return(null);
                        }
                    })
                                   .Where(info => info != null)
                                   .ToArray();

                    return(generics.First().Invoke(null, arguments));
                }
Example #4
0
                public static string CreateTheadName(object context, string alias)
                {
                    XContract.ArgIsNotNull(context, nameof(context));
                    XContract.ArgIsNotNull(alias, nameof(alias));

                    return($"#Task::{context.GetType().Name}->{alias}");
                }
Example #5
0
            public static IEnumerable <Type> WhereIs(IEnumerable <Type> types, Type contractType)
            {
                XContract.ArgIsNotNull(types, nameof(types));
                XContract.ArgIsNotNull(contractType, nameof(contractType));

                return(types.Where(type => Is(contractType, type)));
            }
            public static IEnumerable <T> Subtraction <T>(IEnumerable <T> enumerable1, IEnumerable <T> enumerable2, Comparison <T> compare = null)
            {
                XContract.ArgIsNotNull(enumerable1, nameof(enumerable1));
                XContract.ArgIsNotNull(enumerable2, nameof(enumerable2));

                if (compare == null)
                {
                    compare = (x, y) => Obj.AreEquals(x, y) ? 0 : 1;
                }

                enumerable1 = enumerable1.ToList();

                if (!enumerable1.Any())
                {
                    return(new T[0]);
                }

                enumerable2 = enumerable2.ToList();

                var result = new List <T>();

                ForEach(
                    enumerable1,
                    c =>
                {
                    // ReSharper disable SimplifyLinqExpression
                    if (!enumerable2.Any(x => compare(c, x) == 0))
                    // ReSharper restore SimplifyLinqExpression
                    {
                        result.Add(c);
                    }
                });

                return(result.ToArray());
            }
Example #7
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)));
                    }
                }
Example #8
0
                public static bool MathParameters(MethodInfo method, Type[] typesOfArgs)
                {
                    XContract.ArgIsNotNull(method, nameof(method));
                    XContract.ArgIsNotNull(typesOfArgs, nameof(typesOfArgs));

                    var parameters = method.GetParameters();

                    if (parameters.Length != typesOfArgs.Length)
                    {
                        return(false);
                    }

                    if (typesOfArgs.Length == 0)
                    {
                        return(true);
                    }

                    for (var i = 0; i < parameters.Length; i++)
                    {
                        var argType       = typesOfArgs[i];
                        var parameterInfo = parameters[i];

                        if (IsParameterTypeOf(parameterInfo, argType))
                        {
                            continue;
                        }

                        return(false);
                    }

                    return(true);
                }
Example #9
0
                private static bool MathGenericParameters(MethodInfo methodInfo, Type[] genericTypes)
                {
                    XContract.ArgIsNotNull(methodInfo, nameof(methodInfo));
                    XContract.ArgIsNotNull(genericTypes, nameof(genericTypes));

                    if (!methodInfo.IsGenericMethod)
                    {
                        return(false);
                    }

                    var genericArguments = methodInfo.GetGenericArguments();

                    if (genericArguments.Length != genericTypes.Length)
                    {
                        return(false);
                    }

                    try
                    {
                        methodInfo.MakeGenericMethod(genericTypes);
                        return(true);
                    }
                    catch (Exception)
                    {
                        return(false);
                    }
                }
Example #10
0
                public static object Invoke(object obj, string methodName, params object[] arguments)
                {
                    XContract.ArgIsNotNull(obj, nameof(obj));
                    XContract.ArgIsNotNull(methodName, nameof(methodName));

                    return(Invoke(obj, Get(obj, methodName), arguments));
                }
    // #region Property

    public static PropertyInfo PropertyInfo <T>(this T obj, Expression <Func <T, object> > expression)
    {
        XContract.ArgIsNotNull(obj, nameof(obj));
        XContract.ArgIsNotNull(expression, nameof(expression));

        return(XHelper.Reflections.Properties.Get(obj, XHelper.Expressions.GetMemberName(expression)));
    }
Example #12
0
                public static MethodInfo GetStatic(Type type, string methodName, bool ifNotFoundThrowAnException = true)
                {
                    XContract.ArgIsNotNull(type, nameof(type));
                    XContract.ArgIsNotNull(methodName, nameof(methodName));

                    return(Get(type, methodName, StaticBindingFlags, ifNotFoundThrowAnException: ifNotFoundThrowAnException));
                }
        public EntityActionEventArgs(TEntity entity)
        {
            XContract.ArgIsNotNull(entity, nameof(entity));

            Entity = entity;
            Action = EEntityAction.NotInformed;
        }
Example #14
0
            public static string CreateFileNameOnUserDocuments(FileExtension fileExtension, DateTime?dateTime = null, string prefix = null)
            {
                XContract.ArgIsNotNull(fileExtension, nameof(fileExtension));

                var myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

                return(fileExtension.ChangeExtension(Path.Combine(myDocuments, CreateFileNameFromDateTime(dateTime, prefix))));
            }
        public StateChangedEventArgs(TState oldState, TState newState)
        {
            XContract.ArgIsNotNull(oldState, nameof(oldState));
            XContract.ArgIsNotNull(newState, nameof(newState));

            Old = oldState;
            New = newState;
        }
Example #16
0
        public KeyCache(Func <TKey, TValue> funcCreate)
        {
            XContract.ArgIsNotNull(funcCreate, nameof(funcCreate));

            _funcCreate = funcCreate;

            _data = new ConcurrentDictionary <TKey, TValue>();
        }
Example #17
0
            public static IEnumerable <Type> GetAllInAssembly(Assembly assembly, bool allowHideTypes = false)
            {
                XContract.ArgIsNotNull(assembly, nameof(assembly));

                return(assembly
                       .GetTypes()
                       .Where(type => allowHideTypes || !Reflections.Attributes.HasAttribute <HideTypeAttribute>(type, false)));
            }
Example #18
0
        public SettingsBuilder(string sectionID, string settingID = null, string directory = null)
        {
            XContract.ArgIsNotNull(sectionID, nameof(sectionID));

            directory ??= DirSettings;
            settingID ??= GlobalSettingID;

            InitBuilder(directory, settingID, sectionID);
        }
Example #19
0
            public static void Write([NotNull] string filePath, [NotNull] Action <FileStream> action)
            {
                XContract.ArgIsNotNull(filePath, nameof(filePath));
                XContract.ArgIsNotNull(action, nameof(action));

                using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);

                action(fileStream);
            }
Example #20
0
            public static TReturn IfIsNotNull <T, TReturn>(T obj, Func <T, TReturn> notNullFunc, Action nullAction = null) where T : class
            {
                XContract.ArgIsNotNull(notNullFunc, nameof(notNullFunc));

                if (obj == null)
                {
                    nullAction?.Invoke();

                    return(default);
            public static void ForEach <T>(IEnumerable <T> enumerable, Action <T> action)
            {
                XContract.ArgIsNotNull(enumerable, nameof(enumerable));
                XContract.ArgIsNotNull(action, nameof(action));

                foreach (var item in enumerable)
                {
                    action(item);
                }
            }
Example #22
0
        protected XArgumentTypeException(string argumentName, Type wait, Type received = null)
            : base(string.Format(DefaultMessage, argumentName, wait.FullName, GetTypeName(received)))
        {
            XContract.ArgIsNotNull(argumentName, nameof(argumentName));
            XContract.ArgIsNotNull(wait, nameof(wait));
            XContract.ArgIsNotNull(received, nameof(received));

            ArgumentName = argumentName;
            Wait         = wait;
            Received     = received;
        }
Example #23
0
            public static TResult GetOrDefault <T, TResult>(T obj, Func <T, TResult> func, TResult defaultValue) where T : class
            {
                XContract.ArgIsNotNull(func, nameof(func));

                if (obj == null)
                {
                    return(defaultValue);
                }

                return(func(obj));
            }
Example #24
0
            public static void Write([NotNull] string filePath, [NotNull] Stream stream)
            {
                XContract.ArgIsNotNull(filePath, nameof(filePath));
                XContract.ArgIsNotNull(stream, nameof(stream));

                stream.Seek(0, SeekOrigin.Begin);

                using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);

                stream.CopyTo(fileStream);
            }
Example #25
0
            public static TResult IfIsNotNull <T, TResult>(T obj, Func <T, TResult> notNullAction, Func <TResult> nullAction) where T : class
            {
                XContract.ArgIsNotNull(notNullAction, nameof(notNullAction));
                XContract.ArgIsNotNull(nullAction, nameof(nullAction));

                if (obj == null)
                {
                    return(nullAction());
                }

                return(notNullAction(obj));
            }
Example #26
0
        public FileExtension([Localizable(false)] string value, EFileExtension type, string description = null)
        {
            XContract.ArgIsNotNull(value, nameof(value));

            if (XHelper.Enums.IsIn(type, EFileExtension.Unknown, EFileExtension.MultiExtensions) && string.IsNullOrEmpty(description))
            {
                throw new ArgumentNullException(nameof(description));
            }

            Type        = type;
            Value       = value;
            Description = description ?? XHelper.Enums.GetDisplay(type);
        }
                public static X509Certificate2 RecoverCertificateBySerialNumber(
                    string serialNumber, StoreName storeName = StoreName.My, StoreLocation storeLocation = StoreLocation.LocalMachine)
                {
                    XContract.ArgIsNotNull(serialNumber, nameof(serialNumber));

                    using var store = new X509Store(storeName, storeLocation);
                    store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

                    return(store
                           .Certificates
                           .Cast <X509Certificate2>()
                           .FirstOrDefault(certificate => serialNumber.Equals(certificate.SerialNumber, StringComparison.InvariantCultureIgnoreCase)));
                }
Example #28
0
            public static void IfIsNotNull <T>(T obj, Action <T> notNullAction, Action nullAction = null) where T : class
            {
                XContract.ArgIsNotNull(notNullAction, nameof(notNullAction));

                if (obj == null)
                {
                    nullAction?.Invoke();

                    return;
                }

                notNullAction(obj);
            }
        public VirtualPropertyDescriptor(
            string propertyName, object value, Type dataType,
            string displayName, string category, VirtualTypeDescriptor parent)
            : base(propertyName, null)
        {
            XContract.ArgIsNotNull(dataType, nameof(dataType));

            Value        = value;
            PropertyType = dataType;
            DisplayName  = displayName;
            Category     = category;
            _parent      = parent;
        }
Example #30
0
            public static TResult IfIsGet <T, TResult>(object obj, Func <T, TResult> getAction, TResult defaultValue) where T : class
            {
                XContract.ArgIsNotNull(getAction, nameof(getAction));

                var cast = obj.As <T>();

                if (cast == null)
                {
                    return(defaultValue);
                }

                return(getAction.Invoke(cast));
            }