<commit_before>// Copyright (c) MarinAtanasov. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the project root for license information.
//
using AppBrix.Application;
using AppBrix.Exceptions;
using System;
using System.Linq;

namespace AppBrix
{
    public static class AppBrixExtensions
    {
        #region IApp extensions
        /// <summary>
        /// Unloads and loads the application.
        /// </summary>
        /// <param name="app">The application.</param>
        public static void Restart(this IApp app)
        {
            app.Stop();
            app.Start();
        }

        /// <summary>
        /// Uninitializes and reinitializes the application.
        /// </summary>
        /// <param name="app">The application.</param>
        public static void Reinitialize(this IApp app)
        {
            app.Uninitialize();
            app.Initialize();
        }

        /// <summary>
        /// Resolves an item by its type.
        /// </summary>
        /// <typeparam name="T">The type of the object to be resolved.</typeparam>
        /// <param name="app">The application.</param>
        /// <returns></returns>
        public static T Get<T>(this IApp app) where T : class
        {
            if (typeof(T).IsAssignableFrom(app.GetType()))
            {
                return (T)app;
            }
            else if (typeof(T).IsAssignableFrom(app.AppConfig.GetType()))
            {
                return (T)app.AppConfig;
            }
            else
            {
                return app.Resolver.Resolve<T>();
            }
        }
        #endregion

        #region Types and enums extensions
        /// <summary>
        /// Parses the string and converts it to an enumeration.
        /// </summary>
        /// <typeparam name="T">The type of the enumeration.</typeparam>
        /// <param name="value">The string.</param>
        /// <returns>The enumeration value matching the string.</returns>
        public static T ToEnum<T>(this string value) where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
                throw new ArgumentException("T must be an enumerated type. T is: " + typeof(T).FullName);

            T result;
            Enum.TryParse<T>(value, true, out result);
            return result;
        }

        /// <summary>
        /// Constructs an object with no parameters.
        /// </summary>
        /// <param name="type">The type of the object to be constructed.</param>
        /// <returns>The constructed object.</returns>
        public static object CreateObject(this Type type)
        {
            var constructor = type.GetConstructor(Type.EmptyTypes);
            if (constructor == null)
                throw new DefaultConstructorMissingException("Unable to find constructor for type " + type);
            return constructor.Invoke(null);
        }

        /// <summary><commit_msg>Modified the message when unable to find the default constructor. It will now display the assembly qualified name.<commit_after>// Copyright (c) MarinAtanasov. All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the project root for license information.
//
using AppBrix.Application;
using AppBrix.Exceptions;
using System;
using System.Linq;

namespace AppBrix
{
    public static class AppBrixExtensions
    {
        #region IApp extensions
        /// <summary>
        /// Unloads and loads the application.
        /// </summary>
        /// <param name="app">The application.</param>
        public static void Restart(this IApp app)
        {
            app.Stop();
            app.Start();
        }

        /// <summary>
        /// Uninitializes and reinitializes the application.
        /// </summary>
        /// <param name="app">The application.</param>
        public static void Reinitialize(this IApp app)
        {
            app.Uninitialize();
            app.Initialize();
        }

        /// <summary>
        /// Resolves an item by its type.
        /// </summary>
        /// <typeparam name="T">The type of the object to be resolved.</typeparam>
        /// <param name="app">The application.</param>
        /// <returns></returns>
        public static T Get<T>(this IApp app) where T : class
        {
            if (typeof(T).IsAssignableFrom(app.GetType()))
            {
                return (T)app;
            }
            else if (typeof(T).IsAssignableFrom(app.AppConfig.GetType()))
            {
                return (T)app.AppConfig;
            }
            else
            {
                return app.Resolver.Resolve<T>();
            }
        }
        #endregion

        #region Types and enums extensions
        /// <summary>
        /// Parses the string and converts it to an enumeration.
        /// </summary>
        /// <typeparam name="T">The type of the enumeration.</typeparam>
        /// <param name="value">The string.</param>
        /// <returns>The enumeration value matching the string.</returns>
        public static T ToEnum<T>(this string value) where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
                throw new ArgumentException("T must be an enumerated type. T is: " + typeof(T).FullName);

            T result;
            Enum.TryParse<T>(value, true, out result);
            return result;
        }

        /// <summary>
        /// Constructs an object with no parameters.
        /// </summary>
        /// <param name="type">The type of the object to be constructed.</param>
        /// <returns>The constructed object.</returns>
        public static object CreateObject(this Type type)
        {
            var constructor = type.GetConstructor(Type.EmptyTypes);
            if (constructor == null)
                throw new DefaultConstructorMissingException(
                    "Unable to find constructor for type " + type.AssemblyQualifiedName);
            return constructor.Invoke(null);
        }

        /// <summary>
{'fraction_non_alphanumeric': 0.1166832174776564, 'fraction_numerical': 0.0, 'mean_word_length': 1.6857777777777778, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5258233', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1491, 'n_words': 465}