private MethodDefinition InjectGetStringHashCodeMethod(TypeDefinition type, SystemReferences systemReferences)
        {
            var method = new MethodDefinition("GetStringHashCode", MethodAttributes.Static | MethodAttributes.HideBySig, _typeSystem.Int32Reference);

            method.Parameters.AddRange(
                Parameter.Create("value", _typeSystem.StringReference),
                Parameter.Create("comparer", systemReferences.StringComparer));

            Instruction c1, l1;

            var instructions = method.Body.Instructions;

            instructions.AddRange(
                Instruction.Create(OpCodes.Ldarg_0),
                c1 = Instruction.Create(OpCodes.Nop),
                Instruction.Create(OpCodes.Ldc_I4_0),
                Instruction.Create(OpCodes.Ret),
                l1 = Instruction.Create(OpCodes.Ldarg_1),
                Instruction.Create(OpCodes.Ldarg_0),
                Instruction.Create(OpCodes.Callvirt, systemReferences.StringComparerGetHashCode),
                Instruction.Create(OpCodes.Ret));

            instructions.Replace(c1, Instruction.Create(OpCodes.Brtrue_S, l1));

            method.Body.Optimize();

            type.Methods.Add(method);

            return(method);
        }
        public EquatableWeaver([NotNull] ModuleWeaver moduleWeaver)
        {
            _logger           = moduleWeaver;
            _moduleDefinition = moduleWeaver.ModuleDefinition;
            _systemReferences = moduleWeaver.SystemReferences;
            var hashCodeMethod = InjectStaticHashCodeClass(_moduleDefinition);

            _aggregateHashCodeMethod = InjectAggregateMethod(hashCodeMethod);
            _getHashCode             = InjectGetHashCodeMethod(hashCodeMethod, _systemReferences);
            _getStringHashCode       = InjectGetStringHashCodeMethod(hashCodeMethod, _systemReferences);
        }
        private static MethodDefinition InjectGetHashCodeMethod([NotNull] TypeDefinition type, [NotNull] SystemReferences systemReferences)
        {
            var typeSystem = type.Module.TypeSystem;
            var method     = new MethodDefinition("GetHashCode", MethodAttributes.Static | MethodAttributes.HideBySig, typeSystem.Int32);

            method.Parameters.AddRange(Parameter.Create("value", typeSystem.Object));

            Instruction c1, l1;

            var instructions = method.Body.Instructions;

            instructions.AddRange(
                Instruction.Create(OpCodes.Ldarg_0),
                c1 = Instruction.Create(OpCodes.Nop),
                Instruction.Create(OpCodes.Ldc_I4_0),
                Instruction.Create(OpCodes.Ret),
                l1 = Instruction.Create(OpCodes.Ldarg_0),
                Instruction.Create(OpCodes.Callvirt, systemReferences.ObjectGetHashCode),
                Instruction.Create(OpCodes.Ret));

            instructions.Replace(c1, Instruction.Create(OpCodes.Brtrue_S, l1));

            method.Body.Optimize();

            type.Methods.Add(method);

            return(method);
        }
Beispiel #4
0
        public static void MarkAsCompilerGenerated(this ICustomAttributeProvider target, SystemReferences systemReferences)
        {
            var assemblyName = typeof(ModuleWeaver).Assembly.GetName();
            var version      = assemblyName.Version.ToString();
            var name         = assemblyName.Name;

            var complierGenerated = new CustomAttribute(systemReferences.GeneratedCodeAttributeCtor);

            complierGenerated.ConstructorArguments.Add(new CustomAttributeArgument(systemReferences.TypeSystem.StringReference, name));
            complierGenerated.ConstructorArguments.Add(new CustomAttributeArgument(systemReferences.TypeSystem.StringReference, version));

            var debuggerNonUserCode = new CustomAttribute(systemReferences.DebuggerNonUserCodeAttributeCtor);

            target.CustomAttributes.AddRange(complierGenerated, debuggerNonUserCode);
        }