<commit_before>using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System;
using System.Reflection;
using Vici.Core.Parser;

namespace Vici.Core
{
#if !NETFX_CORE
    public static class TypeInfoMocker
    {
        public static Type GetTypeInfo(this Type type) { return type; }
    }
#endif
    public class TypeInspector
    {
        private Type _t;

        public TypeInspector(Type type)
        {
            _t = type;
        }

#if NETFX_CORE

        private T WalkAndFindSingle<T>(Func<Type,T> f) where T:class 
        {
            Type t = _t;

            while (t != null)
            {
                T result = f(t);

                if (result != null)
                    return result;

                t = t.GetTypeInfo().BaseType;
            }

            return null;
        }

        private T[] WalkAndFindMultiple<T>(Func<Type, IEnumerable<T>> f) where T : class
        {
            List<T> array = new List<T>();

            Type t = _t;

            while (t != null)
            {
                IEnumerable<T> result = f(t);

                if (result != null)
                    array.AddRange(result);

                t = t.GetTypeInfo().BaseType;
            }

            return array.ToArray();
        }

#endif

        public bool IsGenericType
        {
            get { return _t.GetTypeInfo().IsGenericType; }
        }

        public bool IsNullable
        {
            get { return (IsGenericType && _t.GetGenericTypeDefinition() == typeof (Nullable<>)); }
        }

        public bool CanBeNull
        {
            get { return !IsValueType || IsNullable; }
        }

        public Type RealType
        {
            get { return Nullable.GetUnderlyingType(_t) ?? _t; }
        }

        public bool IsPrimitive
        {
            get { return _t.GetTypeInfo().IsPrimitive; }
        }

        public bool IsValueType
        {
            get { return _t.GetTypeInfo().IsValueType; }
        }

        public Type BaseType
        {
            get { return _t.GetTypeInfo().BaseType; }
        }

        public bool IsEnum
        {
            get { return _t.GetTypeInfo().IsEnum; }
        }

        public object DefaultValue()
        {
            if (CanBeNull)
                return null;

            return Activator.CreateInstance(_t);
        }

        public MethodInfo GetMethod(string name, Type[] types)
        {
#if NETFX_CORE
            return WalkAndFindSingle(t => t.GetTypeInfo().GetDeclaredMethods(name).FirstOrDefault(mi => types.SequenceEqual(mi.GetParameters().Select(p => p.ParameterType))));
#else
            return _t.GetMethod(name, types);
#endif
        }

        public bool HasAttribute<T>(bool inherit) where T : Attribute
        {
            return _t.GetTypeInfo().IsDefined(typeof(T), inherit);
        }

        public T GetAttribute<T>(bool inherit) where T : Attribute
        {
            return _t.GetTypeInfo().GetCustomAttributes<T>(inherit).FirstOrDefault();
        }

        public T[] GetAttributes<T>(bool inherit) where T : Attribute
        {
            return (T[])_t.GetTypeInfo().GetCustomAttributes(typeof(T), inherit).ToArray();
        }

        public bool IsAssignableFrom(Type type)
        {
            return _t.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo());
        }

        public ConstructorInfo[] GetConstructors()
        {
#if NETFX_CORE
            return _t.GetTypeInfo().DeclaredConstructors.ToArray();
#else
            return _t.GetConstructors();
#endif
        }

        public MemberInfo[] GetMember(string propertyName)
        {
#if NETFX_CORE
            return WalkAndFindMultiple(t => t.GetTypeInfo().DeclaredMembers.Where(m => m.Name == propertyName));
#else
            return _t.GetMember(propertyName);
#endif
        }

        public PropertyInfo GetIndexer(Type[] types)
        {
#if NETFX_CORE
            return WalkAndFindSingle(t => t.GetTypeInfo().DeclaredProperties.FirstOrDefault(pi => pi.Name == "Item" && LazyBinder.ParametersMatch(types, pi.GetIndexParameters())));
#else            
            return _t.GetProperty("Item", new[] { typeof(string) });
#endif
        }

        public T[] GetCustomAttributes<T>(bool inherit) where T:Attribute
        {
#if NETFX_CORE
            var attributes = _t.GetTypeInfo().GetCustomAttributes<T>(inherit).ToArray();
            return attributes;
#else
            return (T[]) _t.GetCustomAttributes(type, inherit);
#endif
        }

        public MethodInfo GetPropertyGetter(string propertyName, Type[] parameterTypes)
        {
            return GetMethod("get_" + propertyName, parameterTypes);
        }

        public PropertyInfo GetProperty(string propName)
        {
#if NETFX_CORE
            return WalkAndFindSingle(t => t.GetTypeInfo().GetDeclaredProperty(propName));
#else<commit_msg>Fixes to generic attribute functions<commit_after>using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System;
using System.Reflection;
using Vici.Core.Parser;

namespace Vici.Core
{
#if !NETFX_CORE
    public static class TypeInfoMocker
    {
        public static Type GetTypeInfo(this Type type) { return type; }
    }
#endif
    public class TypeInspector
    {
        private Type _t;

        public TypeInspector(Type type)
        {
            _t = type;
        }

#if NETFX_CORE

        private T WalkAndFindSingle<T>(Func<Type,T> f) where T:class 
        {
            Type t = _t;

            while (t != null)
            {
                T result = f(t);

                if (result != null)
                    return result;

                t = t.GetTypeInfo().BaseType;
            }

            return null;
        }

        private T[] WalkAndFindMultiple<T>(Func<Type, IEnumerable<T>> f) where T : class
        {
            List<T> array = new List<T>();

            Type t = _t;

            while (t != null)
            {
                IEnumerable<T> result = f(t);

                if (result != null)
                    array.AddRange(result);

                t = t.GetTypeInfo().BaseType;
            }

            return array.ToArray();
        }

#endif

        public bool IsGenericType
        {
            get { return _t.GetTypeInfo().IsGenericType; }
        }

        public bool IsNullable
        {
            get { return (IsGenericType && _t.GetGenericTypeDefinition() == typeof (Nullable<>)); }
        }

        public bool CanBeNull
        {
            get { return !IsValueType || IsNullable; }
        }

        public Type RealType
        {
            get { return Nullable.GetUnderlyingType(_t) ?? _t; }
        }

        public bool IsPrimitive
        {
            get { return _t.GetTypeInfo().IsPrimitive; }
        }

        public bool IsValueType
        {
            get { return _t.GetTypeInfo().IsValueType; }
        }

        public Type BaseType
        {
            get { return _t.GetTypeInfo().BaseType; }
        }

        public bool IsEnum
        {
            get { return _t.GetTypeInfo().IsEnum; }
        }

        public object DefaultValue()
        {
            if (CanBeNull)
                return null;

            return Activator.CreateInstance(_t);
        }

        public MethodInfo GetMethod(string name, Type[] types)
        {
#if NETFX_CORE
            return WalkAndFindSingle(t => t.GetTypeInfo().GetDeclaredMethods(name).FirstOrDefault(mi => types.SequenceEqual(mi.GetParameters().Select(p => p.ParameterType))));
#else
            return _t.GetMethod(name, types);
#endif
        }

        public bool HasAttribute<T>(bool inherit) where T : Attribute
        {
            return _t.GetTypeInfo().IsDefined(typeof(T), inherit);
        }

        public T GetAttribute<T>(bool inherit) where T : Attribute
        {
#if NETFX_CORE        
            return _t.GetTypeInfo().GetCustomAttributes<T>(inherit).FirstOrDefault();
#else
			return (T) _t.GetCustomAttributes(typeof(T),inherit).FirstOrDefault();
#endif            
        }

        public T[] GetAttributes<T>(bool inherit) where T : Attribute
        {
            return (T[])_t.GetTypeInfo().GetCustomAttributes(typeof(T), inherit).ToArray();
        }

        public bool IsAssignableFrom(Type type)
        {
            return _t.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo());
        }

        public ConstructorInfo[] GetConstructors()
        {
#if NETFX_CORE
            return _t.GetTypeInfo().DeclaredConstructors.ToArray();
#else
            return _t.GetConstructors();
#endif
        }

        public MemberInfo[] GetMember(string propertyName)
        {
#if NETFX_CORE
            return WalkAndFindMultiple(t => t.GetTypeInfo().DeclaredMembers.Where(m => m.Name == propertyName));
#else
            return _t.GetMember(propertyName);
#endif
        }

        public PropertyInfo GetIndexer(Type[] types)
        {
#if NETFX_CORE
            return WalkAndFindSingle(t => t.GetTypeInfo().DeclaredProperties.FirstOrDefault(pi => pi.Name == "Item" && LazyBinder.ParametersMatch(types, pi.GetIndexParameters())));
#else            
            return _t.GetProperty("Item", new[] { typeof(string) });
#endif
        }

        public T[] GetCustomAttributes<T>(bool inherit) where T:Attribute
        {
#if NETFX_CORE
            return _t.GetTypeInfo().GetCustomAttributes<T>(inherit).ToArray();;
#else
            return (T[]) _t.GetCustomAttributes(typeof(T), inherit);
#endif
        }

        public MethodInfo GetPropertyGetter(string propertyName, Type[] parameterTypes)
        {
            return GetMethod("get_" + propertyName, parameterTypes);
        }

        public PropertyInfo GetProperty(string propName)
        {
#if NETFX_CORE
            return WalkAndFindSingle(t => t.GetTypeInfo().GetDeclaredProperty(propName));
#else
{'fraction_non_alphanumeric': 0.10352512155591573, 'fraction_numerical': 0.0, 'mean_word_length': 1.8575976845151954, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 49, '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': '1425931', 'n_tokens_mistral': 2720, 'n_tokens_neox': 2675, 'n_words': 597}