<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Discord.Commands
{
    internal class ReflectionUtils
    {
        internal static object CreateObject(TypeInfo typeInfo, CommandService service, IDependencyMap map = null)
        {
            if (typeInfo.DeclaredConstructors.Count() > 1)
                throw new InvalidOperationException($"Found too many constructors for \"{typeInfo.FullName}\"");

            var constructor = typeInfo.DeclaredConstructors.FirstOrDefault();

            if (constructor == null)
                throw new InvalidOperationException($"Found no constructor for \"{typeInfo.FullName}\"");

            object[] parameters;
            try
            {
                // TODO: probably change this ternary into something sensible
                parameters = constructor.GetParameters()
                    .Select(x => x.ParameterType == typeof(CommandService) ? service : map.Get(x.ParameterType)).ToArray();
            }
            catch (KeyNotFoundException ex) // tried to inject an invalid dependency
            {
                throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (could not provide parameter)", ex);
            }
            catch (NullReferenceException ex) // tried to find a dependency
            {
                throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (type requires dependency injection)", ex);
            }

            try
            {
                return constructor.Invoke(parameters);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (Error invoking constructor)", ex);
            }
        }
    }
}<commit_msg>Allow users to get IDependencyMap if they follow a strict format<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Discord.Commands
{
    internal class ReflectionUtils
    {
        internal static object CreateObject(TypeInfo typeInfo, CommandService service, IDependencyMap map = null)
        {
            if (typeInfo.DeclaredConstructors.Count() > 1)
                throw new InvalidOperationException($"Found too many constructors for \"{typeInfo.FullName}\"");

            var constructor = typeInfo.DeclaredConstructors.FirstOrDefault();

            if (constructor == null)
                throw new InvalidOperationException($"Found no constructor for \"{typeInfo.FullName}\"");

            object[] arguments = null;

            ParameterInfo[] parameters = constructor.GetParameters();

            // TODO: can this logic be made better/cleaner?
            if (parameters.Length == 1)
            {
                if (parameters[0].ParameterType == typeof(IDependencyMap))
                {
                    if (map != null)
                        arguments = new object[] { map };
                    else
                        throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (an IDependencyMap is required)");
                }
            }
            else if (parameters.Length == 2)
            {
                if (parameters[0].ParameterType == typeof(CommandService) && parameters[1].ParameterType == typeof(IDependencyMap))
                    if (map != null)
                        arguments = new object[] { service, map };
                    else
                        throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (an IDependencyMap is required)");
            }

            if (arguments == null)
            {
                try
                {
                    // TODO: probably change this ternary into something sensible?
                    arguments = parameters.Select(x => x.ParameterType == typeof(CommandService) ? service : map.Get(x.ParameterType)).ToArray();
                }
                catch (KeyNotFoundException ex) // tried to inject an invalid dependency
                {
                    throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (could not provide parameter)", ex);
                }
                catch (NullReferenceException ex) // tried to find a dependency
                {
                    throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (an IDependencyMap is required)", ex);
                }
            }

            try
            {
                return constructor.Invoke(arguments);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"Could not find a valid constructor for \"{typeInfo.FullName}\" (Error invoking constructor)", ex);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08919348430671434, 'fraction_numerical': 0.0013905442987683751, 'mean_word_length': 1.8656801365964713, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6071302', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1095, 'n_words': 347}