public static MethodReference InjectEqualsInternal( TypeDefinition type, TypeReference typeRef, MethodDefinition collectionEquals ) { var methodAttributes = MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.Static; var method = new MethodDefinition("EqualsInternal", methodAttributes, ReferenceFinder.Boolean.TypeReference); method.CustomAttributes.MarkAsGeneratedCode(); var left = method.Parameters.Add("left", typeRef); var right = method.Parameters.Add("right", typeRef); var body = method.Body; var ins = body.Instructions; var properties = type.GetPropertiesWithoutIgnores(ignoreAttributeName); foreach (var property in properties) { AddPropertyCode( type, collectionEquals, property, ins); } AddReturnTrue(ins); body.OptimizeMacros(); type.Methods.Add(method); var methodToCall = new MethodReference(method.Name, method.ReturnType, typeRef); foreach (var parameter in method.Parameters) { methodToCall.Parameters.Add(parameter); } return methodToCall; }
public static MethodDefinition Inject( TypeDefinition type ) { var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual; var method = new MethodDefinition("GetHashCode", methodAttributes, ReferenceFinder.Int32.TypeReference); method.CustomAttributes.MarkAsGeneratedCode(); var resultVariable = method.Body.Variables.Add("result", ReferenceFinder.Int32.TypeReference); var body = method.Body; var ins = body.Instructions; ins.Add(Instruction.Create(OpCodes.Ldc_I4_0)); ins.Add(Instruction.Create(OpCodes.Stloc, resultVariable)); var properties = type.GetPropertiesWithoutIgnores(ignoreAttributeName); if (properties.Length == 0) { AddResutInit(ins, resultVariable); } var isFirst = true; foreach (var property in properties) { AddPropertyCode(property, isFirst, ins, resultVariable, method, type); isFirst = false; } AddReturnCode(ins, resultVariable); body.OptimizeMacros(); type.Methods.Add(method); return method; }
public static MethodReference InjectEqualsInternal(TypeDefinition type, TypeReference typeRef, MethodDefinition collectionEquals) { var methodAttributes = MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.Static; var method = new MethodDefinition("EqualsInternal", methodAttributes, ReferenceFinder.Boolean.TypeReference); method.CustomAttributes.MarkAsGeneratedCode(); var left = method.Parameters.Add("left", typeRef); var right = method.Parameters.Add("right", typeRef); var body = method.Body; body.InitLocals = true; var ins = body.Instructions; var properties = type.GetPropertiesWithoutIgnores(ignoreAttributeName); foreach (var property in properties) { AddPropertyCode(type, collectionEquals, property, ins, left, right); } var methods = type.GetMethods(); var customLogic = methods .Where(x => x.CustomAttributes.Any(y => y.AttributeType.Name == customAttribute)).ToArray(); if (customLogic.Length > 2) { throw new WeavingException("Only one custom method can be specified."); } if (customLogic.Length == 1) { AddCustomLogicCall(type, body, ins, customLogic); } AddReturnTrue(ins); body.OptimizeMacros(); type.Methods.AddOrReplace(method); var methodToCall = new MethodReference(method.Name, method.ReturnType, typeRef); foreach (var parameter in method.Parameters) { methodToCall.Parameters.Add(parameter); } return methodToCall; }