static void AddSimpleValueCheck(Collection<Instruction> c, PropertyDefinition property, TypeDefinition type, ParameterDefinition left, ParameterDefinition right) { var getMethod = property.GetGetMethod(type); var getMethodImported = ReferenceFinder.ImportCustom(getMethod); c.Add(Instruction.Create(type.GetLdArgForType(), left)); c.Add(Instruction.Create(getMethodImported.GetCallForMethod(), getMethodImported)); c.Add(Instruction.Create(type.GetLdArgForType(), right)); c.Add(Instruction.Create(getMethodImported.GetCallForMethod(), getMethodImported)); c.Add(Instruction.Create(OpCodes.Ceq)); }
static void AddCollectionEquals(TypeDefinition type, MethodDefinition collectionEquals, PropertyDefinition property, TypeDefinition propType, Collection<Instruction> e, ParameterDefinition left, ParameterDefinition right) { e.If( cf => { cf.Add(Instruction.Create(type.GetLdArgForType(), right)); cf.Add(Instruction.Create(OpCodes.Ldnull)); cf.Add(Instruction.Create(OpCodes.Ceq)); }, t => t.Add(Instruction.Create(OpCodes.Ldc_I4_0)), es => { var getMethod = property.GetGetMethod(type); var getMethodImported = ReferenceFinder.ImportCustom(getMethod); es.Add(Instruction.Create(type.GetLdArgForType(), left)); es.Add(Instruction.Create(getMethodImported.GetCallForMethod(), getMethodImported)); if (propType.IsValueType) { es.Add(Instruction.Create(OpCodes.Box, propType)); } es.Add(Instruction.Create(type.GetLdArgForType(), right)); es.Add(Instruction.Create(getMethodImported.GetCallForMethod(), getMethodImported)); if (propType.IsValueType) { es.Add(Instruction.Create(OpCodes.Box, propType)); } es.Add(Instruction.Create(OpCodes.Call, collectionEquals)); }); }
static void AddNormalCheck(TypeDefinition type, Collection<Instruction> c, PropertyDefinition property, ParameterDefinition left, ParameterDefinition right) { var genericInstance = new Lazy<TypeReference>(() => property.PropertyType.GetGenericInstanceType(type)); var getMethodImported = ReferenceFinder.ImportCustom(property.GetGetMethod(type)); c.Add(Instruction.Create(type.GetLdArgForType(), left)); c.Add(Instruction.Create(getMethodImported.GetCallForMethod(), getMethodImported)); if (property.PropertyType.IsValueType || property.PropertyType.IsGenericParameter) { c.Add(Instruction.Create(OpCodes.Box, genericInstance.Value)); } c.Add(Instruction.Create(type.GetLdArgForType(), right)); c.Add(Instruction.Create(getMethodImported.GetCallForMethod(), getMethodImported)); if (property.PropertyType.IsValueType || property.PropertyType.IsGenericParameter) { c.Add(Instruction.Create(OpCodes.Box, genericInstance.Value)); } c.Add(Instruction.Create(OpCodes.Call, ReferenceFinder.Object.StaticEquals)); }
static VariableDefinition AddNullableProperty(PropertyDefinition property, Collection<Instruction> ins, TypeDefinition type, VariableDefinition variable) { ins.If(c => { var nullablePropertyResolved = property.PropertyType.Resolve(); var nullablePropertyImported = ReferenceFinder.ImportCustom(property.PropertyType); ins.Add(Instruction.Create(OpCodes.Ldarg_0)); var getMethod = ReferenceFinder.ImportCustom(property.GetGetMethod(type)); c.Add(Instruction.Create(OpCodes.Call, getMethod)); variable = new VariableDefinition(getMethod.ReturnType); c.Add(Instruction.Create(OpCodes.Stloc, variable)); c.Add(Instruction.Create(OpCodes.Ldloca, variable)); var hasValuePropertyResolved = nullablePropertyResolved.Properties.First(x => x.Name == "HasValue").Resolve(); var hasMethod = ReferenceFinder.ImportCustom(hasValuePropertyResolved.GetGetMethod(nullablePropertyImported)); c.Add(Instruction.Create(OpCodes.Call, hasMethod)); }, t => { var nullableProperty = ReferenceFinder.ImportCustom(property.PropertyType); t.Add(Instruction.Create(OpCodes.Ldarg_0)); var imp = property.GetGetMethod(type); var imp2 = ReferenceFinder.ImportCustom(imp); t.Add(Instruction.Create(OpCodes.Call, imp2)); t.Add(Instruction.Create(OpCodes.Box, nullableProperty)); t.Add(Instruction.Create(OpCodes.Callvirt, ReferenceFinder.Object.GetHashcode)); }, e => e.Add(Instruction.Create(OpCodes.Ldc_I4_0))); return variable; }
static void LoadVariable(PropertyDefinition property, Collection<Instruction> ins, TypeDefinition type) { var get = property.GetGetMethod(type); var imported = ReferenceFinder.ImportCustom(get); ins.Add(Instruction.Create(OpCodes.Ldarg_0)); ins.Add(Instruction.Create(OpCodes.Call, imported)); }
private static void AddSimpleValueCheck(Collection<Instruction> c, PropertyDefinition property, TypeDefinition type) { c.Add(Instruction.Create(OpCodes.Ldarg_0)); c.Add(Instruction.Create(OpCodes.Callvirt, property.GetGetMethod(type))); c.Add(Instruction.Create(OpCodes.Ldarg_1)); c.Add(Instruction.Create(OpCodes.Callvirt, property.GetGetMethod(type))); c.Add(Instruction.Create(OpCodes.Ceq)); }
private static void AddNormalCheck(TypeDefinition type, Collection<Instruction> c, PropertyDefinition property, TypeDefinition propType) { c.Add(Instruction.Create(OpCodes.Ldarg_0)); c.Add(Instruction.Create(OpCodes.Callvirt, property.GetGetMethod(type))); if (property.PropertyType.IsValueType) { c.Add(Instruction.Create(OpCodes.Box, property.PropertyType)); } c.Add(Instruction.Create(OpCodes.Ldarg_1)); c.Add(Instruction.Create(OpCodes.Callvirt, property.GetGetMethod(type))); if (property.PropertyType.IsValueType) { c.Add(Instruction.Create(OpCodes.Box, property.PropertyType)); } c.Add(Instruction.Create(OpCodes.Call, ReferenceFinder.Object.StaticEquals)); }
private static void AddCollectionEquals(TypeDefinition type, MethodDefinition collectionEquals, PropertyDefinition property, TypeDefinition propType, Collection<Instruction> e) { e.If( cf => { cf.Add(Instruction.Create(OpCodes.Ldarg_1)); cf.Add(Instruction.Create(OpCodes.Ldnull)); cf.Add(Instruction.Create(OpCodes.Ceq)); }, t => { t.Add(Instruction.Create(OpCodes.Ldc_I4_0)); }, es => { es.Add(Instruction.Create(OpCodes.Ldarg_0)); es.Add(Instruction.Create(OpCodes.Callvirt, property.GetGetMethod(type))); if (propType.IsValueType) { es.Add( Instruction.Create( OpCodes.Box, propType ) ); } es.Add(Instruction.Create(OpCodes.Ldarg_1)); es.Add(Instruction.Create(OpCodes.Callvirt, property.GetGetMethod(type))); if (propType.IsValueType) { es.Add( Instruction.Create( OpCodes.Box, propType ) ); } es.Add(Instruction.Create(OpCodes.Call, collectionEquals)); }); }
private void AddPropertyCode(MethodBody body, int index, PropertyDefinition property, TypeDefinition targetType, Collection<VariableDefinition> variables) { var ins = body.Instructions; ins.Add(Instruction.Create(OpCodes.Ldloc_0)); ins.Add(Instruction.Create(OpCodes.Ldc_I4, index)); var get = ModuleDefinition.Import(property.GetGetMethod(targetType)); ins.Add(Instruction.Create(OpCodes.Ldarg_0)); ins.Add(Instruction.Create(OpCodes.Call, get)); if ( get.ReturnType.IsValueType) { var returnType = ModuleDefinition.Import(property.GetMethod.ReturnType); if( returnType.FullName == "System.DateTime" ) { var convertToUtc = ModuleDefinition.Import(returnType.Resolve().FindMethod( "ToUniversalTime" )); var variable = new VariableDefinition(returnType); variables.Add(variable); ins.Add(Instruction.Create(OpCodes.Stloc, variable)); ins.Add(Instruction.Create(OpCodes.Ldloca, variable)); ins.Add(Instruction.Create(OpCodes.Call, convertToUtc)); } ins.Add(Instruction.Create(OpCodes.Box, returnType)); } else { var propType = property.PropertyType.Resolve(); var isCollection = !property.PropertyType.IsGenericParameter && propType.IsCollection(); if (isCollection) { AssignFalseToFirstFLag(ins); If(ins, nc => nc.Add(Instruction.Create(OpCodes.Dup)), nt => { GetEnumerator(nt); NewStringBuilder(nt); AppendString(nt, "["); While(nt, c => { c.Add(Instruction.Create(OpCodes.Ldloc_2)); c.Add(Instruction.Create(OpCodes.Callvirt, moveNext)); }, b => { AppendSeparator(b, appendString); ins.Add(Instruction.Create(OpCodes.Ldloc_1)); If(ins, c => { c.Add(Instruction.Create(OpCodes.Ldloc_2)); c.Add(Instruction.Create(OpCodes.Callvirt, current)); }, t => { t.Add(Instruction.Create(OpCodes.Call, getInvariantCulture)); string format; var collectionType = ((GenericInstanceType)property.PropertyType).GenericArguments[0]; if (HaveToAddQuotes(collectionType)) { format = "\"{0}\""; } else { format = "{0}"; } t.Add(Instruction.Create(OpCodes.Ldstr, format)); t.Add(Instruction.Create(OpCodes.Ldc_I4, 1)); t.Add(Instruction.Create(OpCodes.Newarr, ModuleDefinition.TypeSystem.Object)); t.Add(Instruction.Create(OpCodes.Stloc, body.Variables[4])); t.Add(Instruction.Create(OpCodes.Ldloc, body.Variables[4])); t.Add(Instruction.Create(OpCodes.Ldc_I4_0)); t.Add(Instruction.Create(OpCodes.Ldloc_2)); t.Add(Instruction.Create(OpCodes.Callvirt, current)); t.Add(Instruction.Create(OpCodes.Stelem_Ref)); t.Add(Instruction.Create(OpCodes.Ldloc, body.Variables[4])); t.Add(Instruction.Create(OpCodes.Call, formatMethod)); }, e => e.Add(Instruction.Create(OpCodes.Ldstr, "null"))); ins.Add(Instruction.Create(OpCodes.Callvirt, appendString)); ins.Add(Instruction.Create(OpCodes.Pop)); }); AppendString(ins, "]"); StringBuilderToString(ins); }, nf => { ins.Add(Instruction.Create(OpCodes.Pop)); ins.Add(Instruction.Create(OpCodes.Ldstr, "null")); }); } else { If(ins, c => { ins.Add(Instruction.Create(OpCodes.Dup)); AddBoxing(property, targetType, c); }, t => AddBoxing(property, targetType, t), e => { ins.Add(Instruction.Create(OpCodes.Pop)); ins.Add(Instruction.Create(OpCodes.Ldstr, "null")); }); } } ins.Add(Instruction.Create(OpCodes.Stelem_Ref)); }
private static void LoadVariable(PropertyDefinition property, Collection<Instruction> ins, TypeDefinition type) { var get = property.GetGetMethod(type); ins.Add(Instruction.Create(OpCodes.Ldarg_0)); ins.Add(Instruction.Create(OpCodes.Call, get)); }