public DynamicReadTravellerBuilder(MethodBuilder builder, SerializableType target, TravellerContext context) { _target = target; _context = context; _il = builder.IL; _visitorVariable = new MethodArgILCodeVariable(1, typeof(IReadVisitor)); }
private void GenerateDictionaryCode(ILCodeVariable dictionary, Type elementType) { var enumerateCode = new ILEnumerateCode(dictionary, (il, it) => { GenerateEnumerateContentCode(new InstancePropertyILCodeVariable(it, elementType.GetProperty("Key")), LevelType.DictionaryKey); GenerateEnumerateContentCode(new InstancePropertyILCodeVariable(it, elementType.GetProperty("Value")), LevelType.DictionaryValue); }); _il.Generate(enumerateCode); }
public void Increment(ILCodeVariable value, ILCodeParameter valueToAdd) { if (value == null) throw new ArgumentNullException("value"); if (valueToAdd == null) throw new ArgumentNullException("valueToAdd"); _il.Var.Load(value); ((IILCodeParameter) valueToAdd).Load(_il); _il.Gen.Emit(OpCodes.Add); _il.Var.Set(value); }
public void Set(ILCodeVariable variable) { if (variable == null) throw new ArgumentNullException("variable"); ((IILCodeVariable)variable).Set(_il); }
public InstancePropertyILCodeVariable Property(ILCodeVariable instance, PropertyInfo property) { return new InstancePropertyILCodeVariable(instance, property); }
public void LoadAddress(ILCodeVariable variable) { if (variable == null) throw new ArgumentNullException("variable"); ((IILCodeVariable)variable).GetAddress(_il); }
public InstanceFieldILCodeVariable Field(ILCodeVariable instance, FieldInfo field) { return new InstanceFieldILCodeVariable(instance, field); }
public InstanceFieldILCodeVariable(ILCodeVariable instance, FieldInfo info) : base(info.FieldType) { _instance = instance; _info = info; }
public static ILCodeParameter Of(ILCodeVariable variable) { return new ILCodeParameterDelegatable(variable.VariableType, il => il.Var.Load(variable), il => il.Var.LoadAddress(variable)); }
public void GetPropertyValue(ILCodeVariable instance, PropertyInfo property) { if (!property.CanRead) throw new InvalidOperationException("Can not get value from a property with no getter"); var getMethod = property.GetGetMethod(); InvokeMethod(instance, getMethod); }
public void GetField(ILCodeVariable instance, FieldInfo field) { _il.Var.Load(instance); _il.LoadField(field); }
public void SetVariable(ILCodeVariable variable, ILCodeParameter valueToSet) { if (valueToSet == null) valueToSet = ILCodeParameter.Null; ((IILCodeParameter) valueToSet).Load(_il); _il.Var.Set(variable); }
public void TransferIfNull(ILCodeVariable reference, Label label) { _var.Load(reference); LoadNull(); CompareEquals(); TransferLongIfTrue(label); }
public InstancePropertyILCodeVariable(ILCodeVariable instance, PropertyInfo info) : base(info.PropertyType) { _instance = instance; _info = info; }
private void GenerateCreateAndChildCallCode(ILCodeVariable local) { var type = local.VariableType; var constructor = type.GetConstructor(Type.EmptyTypes); if (constructor == null) throw InvalidGraphException.NoParameterLessConstructor(type); _il.Construct(constructor); _il.Var.Set(local); var childTravellerInfo = _context.GetTraveller(type); var field = _il.Var.Field(_il.Var.This(), childTravellerInfo.Field); _il.Snippets.InvokeMethod(field, childTravellerInfo.TravelReadMethod, _visitorVariable, local); }
public void SetPropertyValue(ILCodeVariable instance, PropertyInfo property, ILCodeParameter value) { if (!property.CanWrite) throw new InvalidOperationException("Can not set value from a property with no setter"); if (value == null) value = ILCodeParameter.Null; var setMethod = property.GetSetMethod(); InvokeMethod(instance, setMethod, value); }
private void GeneratePropertyCode(ILCodeVariable graphVariable, SerializableProperty target) { var extPropertyType = target.Ext; var argsField = _context.GetArgsField(target); var argsFieldVariable = _il.Var.Field(_il.Var.This(), argsField); if (target.Ext.IsValueOrNullableOfValue()) { var valueType = target.Ext.IsEnum() ? target.Ext.GetUnderlyingEnumType() : target.Ref.PropertyType; var propertyParameter = _il.Var.Property(graphVariable, target.Ref).AsNullable(); _il.Snippets.InvokeMethod(_visitorVariable, Members.VisitorVisitValue[valueType], propertyParameter, argsFieldVariable); } else if (extPropertyType.Class == TypeClass.Dictionary) { var container = extPropertyType.Container.AsDictionary(); var dictionaryType = container.DictionaryInterfaceType; var cLocal = _il.DeclareLocal("dictionary", dictionaryType); _il.Snippets.SetVariable(cLocal, _il.Var.Property(graphVariable, target.Ref).Cast(dictionaryType)); _il.Snippets.InvokeMethod(_visitorVariable, Members.VisitorVisit, cLocal, argsFieldVariable); _il.IfNotEqual(cLocal, null) .Then(() => GenerateDictionaryCode(cLocal, container.ElementType)) .End(); _il.Snippets.InvokeMethod(_visitorVariable, Members.VisitorLeave, cLocal, argsFieldVariable); } else if (extPropertyType.Class == TypeClass.Collection) { var container = extPropertyType.Container.AsCollection(); var collectionType = extPropertyType.Ref.IsArray && extPropertyType.Container.AsArray().Ranks > 1 ? extPropertyType.Ref : container.CollectionInterfaceType; var cLocal = _il.DeclareLocal("collection", collectionType); _il.Snippets.SetVariable(cLocal, _il.Var.Property(graphVariable, target.Ref).Cast(collectionType)); _il.Snippets.InvokeMethod(_visitorVariable, Members.VisitorVisit, cLocal, argsFieldVariable); _il.IfNotEqual(cLocal, null).Then( () => GenerateEnumerateCollectionContentCode(extPropertyType, cLocal)) .End(); _il.Snippets.InvokeMethod(_visitorVariable, Members.VisitorLeave, cLocal, argsFieldVariable); } else { var singleLocal = _il.DeclareLocal("single", target.Ref.PropertyType); _il.Snippets.GetPropertyValue(graphVariable, target.Ref); _il.Var.Set(singleLocal); _il.Snippets.InvokeMethod(_visitorVariable, Members.VisitorVisit, singleLocal, argsFieldVariable); var checkIfNullLabel = _il.DefineLabel(); _il.TransferIfNull(singleLocal, checkIfNullLabel); GenerateChildCall(singleLocal); _il.MarkLabel(checkIfNullLabel); _il.Snippets.InvokeMethod(_visitorVariable, Members.VisitorLeave, singleLocal, argsFieldVariable); } }