<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using Solar.Infrastructure.Common.Extensions;

namespace Solar.Infrastructure.Common.StaticServices
{
    internal static class MapFunctionGenerator
    {
        public static Action<TSource, TTarget> Generate<TSource, TTarget>()
            where TTarget : class, new()
        {
            var dynamicMethod = GetDynamicMethod(typeof(TSource), typeof(TTarget));
            return (Action<TSource, TTarget>) dynamicMethod.CreateDelegate(typeof(Action<TSource, TTarget>));
        }

        public static Action<object, object> Generate(Type sourceType, Type targetType)
        {
            var dynamicMethod = GetDynamicMethod(sourceType, targetType);
            //return (Action<object, object>) dynamicMethod.CreateDelegate(typeof(Action<object, object>));
            return (source, target) =>
            {
                var parameters = new[]
                {
                    Convert.ChangeType(source, sourceType),
                    Convert.ChangeType(target, targetType)
                };
                dynamicMethod.Invoke(null, parameters);
            };
        }

        private static DynamicMethod GetDynamicMethod(Type sourceType, Type targetType)
        {
            var dynamicMethod = new DynamicMethod("MappingMethod", null, new[] { sourceType, targetType });
            var ilGenerator = dynamicMethod.GetILGenerator();
            GenerateParameters(dynamicMethod);
            GenerateMapMethodBody(ilGenerator, sourceType, targetType);
            GenerateReturnStatement(ilGenerator);
            return dynamicMethod;
        }

        private static void GenerateParameters(DynamicMethod dynamicMethod)
        {
            dynamicMethod.DefineParameter(1, ParameterAttributes.None, "source");
            dynamicMethod.DefineParameter(2, ParameterAttributes.None, "target");
        }

        private static void GenerateMapMethodBody(ILGenerator generator, Type sourceType, Type targetType)
        {
            foreach (var pair in GetAssignementPropertiesPairs(sourceType, targetType))
            {
                generator.Emit(OpCodes.Ldarg, 1);
                generator.Emit(OpCodes.Ldarg, 0);
                generator.Emit(OpCodes.Callvirt, pair.SourcePropertyGetter);
                generator.Emit(OpCodes.Callvirt, pair.TargetPropertySetter);
            }
        }

        private static void GenerateReturnStatement(ILGenerator generator)
        {
            generator.Emit(OpCodes.Ret);
        }

        private static IEnumerable<AssignmentPropertiesMethods> GetAssignementPropertiesPairs(Type sourceType, Type targetType)
        {
            var sourceProperties = sourceType.GetPublicProperties();
            var targetProperties = targetType.GetPublicProperties();
            return
                from sp in sourceProperties
                let tp = targetProperties.FirstOrDefault(tp => IsMappableProperty(tp, sp))
                where tp != null
                select new AssignmentPropertiesMethods
                {
                    SourcePropertyGetter = sp.GetGetMethod(),
                    TargetPropertySetter = tp.GetSetMethod()
                };
        }

        private static bool IsMappableProperty(PropertyInfo tp, PropertyInfo sp)
        {
            return tp.Name == sp.Name && tp.PropertyType == sp.PropertyType;
        }

        private class AssignmentPropertiesMethods
        {
            public MethodInfo SourcePropertyGetter { get; set; }

            public MethodInfo TargetPropertySetter { get; set; }
        }
    }
}
<commit_msg>Delete dead code in MapFunctionGenerator<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using Solar.Infrastructure.Common.Extensions;

namespace Solar.Infrastructure.Common.StaticServices
{
    internal static class MapFunctionGenerator
    {
        public static Action<TSource, TTarget> Generate<TSource, TTarget>()
            where TTarget : class, new()
        {
            var dynamicMethod = GetDynamicMethod(typeof(TSource), typeof(TTarget));
            return (Action<TSource, TTarget>) dynamicMethod.CreateDelegate(typeof(Action<TSource, TTarget>));
        }

        public static Action<object, object> Generate(Type sourceType, Type targetType)
        {
            var dynamicMethod = GetDynamicMethod(sourceType, targetType);
            return (source, target) =>
            {
                var parameters = new[]
                {
                    Convert.ChangeType(source, sourceType),
                    Convert.ChangeType(target, targetType)
                };
                dynamicMethod.Invoke(null, parameters);
            };
        }

        private static DynamicMethod GetDynamicMethod(Type sourceType, Type targetType)
        {
            var dynamicMethod = new DynamicMethod("MappingMethod", null, new[] { sourceType, targetType });
            var ilGenerator = dynamicMethod.GetILGenerator();
            GenerateParameters(dynamicMethod);
            GenerateMapMethodBody(ilGenerator, sourceType, targetType);
            GenerateReturnStatement(ilGenerator);
            return dynamicMethod;
        }

        private static void GenerateParameters(DynamicMethod dynamicMethod)
        {
            dynamicMethod.DefineParameter(1, ParameterAttributes.None, "source");
            dynamicMethod.DefineParameter(2, ParameterAttributes.None, "target");
        }

        private static void GenerateMapMethodBody(ILGenerator generator, Type sourceType, Type targetType)
        {
            foreach (var pair in GetAssignementPropertiesPairs(sourceType, targetType))
            {
                generator.Emit(OpCodes.Ldarg, 1);
                generator.Emit(OpCodes.Ldarg, 0);
                generator.Emit(OpCodes.Callvirt, pair.SourcePropertyGetter);
                generator.Emit(OpCodes.Callvirt, pair.TargetPropertySetter);
            }
        }

        private static void GenerateReturnStatement(ILGenerator generator)
        {
            generator.Emit(OpCodes.Ret);
        }

        private static IEnumerable<AssignmentPropertiesMethods> GetAssignementPropertiesPairs(Type sourceType, Type targetType)
        {
            var sourceProperties = sourceType.GetPublicProperties();
            var targetProperties = targetType.GetPublicProperties();
            return
                from sp in sourceProperties
                let tp = targetProperties.FirstOrDefault(tp => IsMappableProperty(tp, sp))
                where tp != null
                select new AssignmentPropertiesMethods
                {
                    SourcePropertyGetter = sp.GetGetMethod(),
                    TargetPropertySetter = tp.GetSetMethod()
                };
        }

        private static bool IsMappableProperty(PropertyInfo tp, PropertyInfo sp)
        {
            return tp.Name == sp.Name && tp.PropertyType == sp.PropertyType;
        }

        private class AssignmentPropertiesMethods
        {
            public MethodInfo SourcePropertyGetter { get; set; }

            public MethodInfo TargetPropertySetter { get; set; }
        }
    }
}

{'fraction_non_alphanumeric': 0.07703865592132222, 'fraction_numerical': 0.0010927468925010244, 'mean_word_length': 2.2298191442434936, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, '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': '140240', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1676, 'n_words': 377}