<commit_before>using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

namespace Machine.MassTransitExtensions.InterfacesAsMessages
{
  public class MessageInterfaceImplementations
  {
    private readonly Dictionary<Type, Type> _interfaceToClass = new Dictionary<Type, Type>();
    private readonly Dictionary<Type, Type> _classToInterface = new Dictionary<Type, Type>();
    private AssemblyBuilder _assemblyBuilder;
    private ModuleBuilder _moduleBuilder;

    public void GenerateImplementationsOf(params Type[] types)
    {
      GenerateImplementationsOf(new List<Type>(types));
    }

    public void GenerateImplementationsOf(IEnumerable<Type> types)
    {
      string name = "Messages";
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = name;
      _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
      _moduleBuilder = _assemblyBuilder.DefineDynamicModule(name, name + ".dll");
      foreach (Type type in types)
      {
        if (!type.IsInterface)
        {
          throw new InvalidOperationException();
        }
        GenerateStub(type);
      }
      _assemblyBuilder.Save("Messages.dll");
    }

    private void GenerateStub(Type type)
    {
      string newTypeName = MakeImplementationName(type);
      TypeAttributes attributes = TypeAttributes.Public | TypeAttributes.Serializable;
      TypeBuilder typeBuilder = _moduleBuilder.DefineType(newTypeName, attributes);
      typeBuilder.AddInterfaceImplementation(type);
      foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
      {
        if (!property.CanRead || !property.CanWrite)
        {
          throw new InvalidOperationException();
        }
        FieldBuilder fieldBuilder = typeBuilder.DefineField(MakeFieldName(property), property.PropertyType, FieldAttributes.Private);
        PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(property.Name, PropertyAttributes.None, property.PropertyType, new Type[0]);
        MethodBuilder getMethod = typeBuilder.DefineMethod("get_" + propertyBuilder.Name, MethodAttributes.Virtual | MethodAttributes.Public, property.PropertyType, new Type[0]);
        ILGenerator ilGet = getMethod.GetILGenerator();
        ilGet.Emit(OpCodes.Ldarg_0);
        ilGet.Emit(OpCodes.Ldfld, fieldBuilder);
        ilGet.Emit(OpCodes.Ret);

        propertyBuilder.SetGetMethod(getMethod);
        typeBuilder.DefineMethodOverride(getMethod, property.GetGetMethod());

        MethodBuilder setMethod = typeBuilder.DefineMethod("set_" + propertyBuilder.Name, MethodAttributes.Virtual | MethodAttributes.Public, typeof(void), new Type[] { property.PropertyType });
        ParameterBuilder parameterBuilder = setMethod.DefineParameter(0, ParameterAttributes.None, "value");<commit_msg>Not saving the generated assembly for interface messages.<commit_after>using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

namespace Machine.MassTransitExtensions.InterfacesAsMessages
{
  public class MessageInterfaceImplementations
  {
    private readonly Dictionary<Type, Type> _interfaceToClass = new Dictionary<Type, Type>();
    private readonly Dictionary<Type, Type> _classToInterface = new Dictionary<Type, Type>();
    private AssemblyBuilder _assemblyBuilder;
    private ModuleBuilder _moduleBuilder;

    public void GenerateImplementationsOf(params Type[] types)
    {
      GenerateImplementationsOf(new List<Type>(types));
    }

    public void GenerateImplementationsOf(IEnumerable<Type> types)
    {
      string name = "Messages";
      AssemblyName assemblyName = new AssemblyName();
      assemblyName.Name = name;
      _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
      _moduleBuilder = _assemblyBuilder.DefineDynamicModule(name, name + ".dll");
      foreach (Type type in types)
      {
        if (!type.IsInterface)
        {
          throw new InvalidOperationException();
        }
        GenerateStub(type);
      }
    }

    private void GenerateStub(Type type)
    {
      string newTypeName = MakeImplementationName(type);
      TypeAttributes attributes = TypeAttributes.Public | TypeAttributes.Serializable;
      TypeBuilder typeBuilder = _moduleBuilder.DefineType(newTypeName, attributes);
      typeBuilder.AddInterfaceImplementation(type);
      foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
      {
        if (!property.CanRead || !property.CanWrite)
        {
          throw new InvalidOperationException();
        }
        FieldBuilder fieldBuilder = typeBuilder.DefineField(MakeFieldName(property), property.PropertyType, FieldAttributes.Private);
        PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(property.Name, PropertyAttributes.None, property.PropertyType, new Type[0]);
        MethodBuilder getMethod = typeBuilder.DefineMethod("get_" + propertyBuilder.Name, MethodAttributes.Virtual | MethodAttributes.Public, property.PropertyType, new Type[0]);
        ILGenerator ilGet = getMethod.GetILGenerator();
        ilGet.Emit(OpCodes.Ldarg_0);
        ilGet.Emit(OpCodes.Ldfld, fieldBuilder);
        ilGet.Emit(OpCodes.Ret);

        propertyBuilder.SetGetMethod(getMethod);
        typeBuilder.DefineMethodOverride(getMethod, property.GetGetMethod());

        MethodBuilder setMethod = typeBuilder.DefineMethod("set_" + propertyBuilder.Name, MethodAttributes.Virtual | MethodAttributes.Public, typeof(void), new Type[] { property.PropertyType });
        ParameterBuilder parameterBuilder = setMethod.DefineParameter(0, ParameterAttributes.None, "value");
{'fraction_non_alphanumeric': 0.08817021276595745, 'fraction_numerical': 0.0013617021276595745, 'mean_word_length': 4.227758007117438, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4159752', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1439, 'n_words': 303}