private void CreateProperties(EmitTypeFactory factory, Type originalType, Type adapterType, TypeBuilder typeBldr, PropertyBuilder sourceProperty, PropertyBuilder factoryProperty) { var getSource = ExecBld.GetProp(sourceProperty, ExecBld.GetThis()); var getFactory = ExecBld.GetProp(factoryProperty, ExecBld.GetThis()); var getMethod = this.GetType().GetMethod(nameof(GetValue)); var setMethod = this.GetType().GetMethod(nameof(SetValue)); var properties = new List <PropertyInfo>(); int index = 0; foreach (var t in adapterType.GetAllInterfaceProperties()) { var tProp = originalType.GetProperty(t.Name); //, t.PropertyType); properties.Add(tProp); var get = ExecBld.Return( ExecBld.Cast( ExecBld.Call(getMethod, getFactory, getSource, ExecBld.GetConst(index)), typeof(object), tProp.PropertyType)); var set = ExecBld.Return( ExecBld.Call(setMethod, getFactory, getSource, ExecBld.GetConst(index), ExecBld.Cast(ExecBld.GetArg(1), tProp.PropertyType, typeof(object))) ); factory.AddProperty(typeBldr, t, get, set, t.DeclaringType); index++; } Properties = properties.ToArray(); }
public InternalDtoAdapterFactory(EmitTypeFactory factory, Type originalType, Type adapterType) { var unique = Guid.NewGuid(); var typeBldr = factory.CreateClass( $"generatedInternalAdapter_{originalType.Name}_{adapterType.Name}_{unique}", null, adapterType, typeof(IDtoAdapter)); var sourcePropertyName = $"Source_{unique}"; var sourceProperty = factory.AddProperty(typeBldr, sourcePropertyName, originalType); var factoryPropertyName = $"Factory_{unique}"; var factoryProperty = factory.AddProperty(typeBldr, factoryPropertyName, typeof(InternalDtoAdapterFactory)); CreateProperties(factory, originalType, adapterType, typeBldr, sourceProperty, factoryProperty); CreateConstructor(factory, originalType, typeBldr, sourceProperty, factoryProperty); CreateIDtoAdapter(factory, typeBldr, sourceProperty); m_constructor = typeBldr.CreateTypeInfo().GetConstructor(new[] { originalType, typeof(InternalDtoAdapterFactory) }); }
private void CreateProperties(EmitTypeFactory factory, Type adapterType, TypeBuilder typeBldr) { foreach (var t in adapterType.GetAllInterfaceProperties()) { var fieldName = "_" + t.Name; var field = typeBldr.DefineField(fieldName, t.PropertyType, FieldAttributes.Private); var get = ExecBld.Return(ExecBld.GetFld(field, ExecBld.GetThis())); var set = ExecBld.Return(ExecBld.SetFld(field, ExecBld.GetThis(), ExecBld.GetArg(1))); factory.AddProperty(typeBldr, t, get, set, t.DeclaringType); } }
private void CreateProperties(EmitTypeFactory factory, Type originalType, Type adapterType, TypeBuilder typeBldr, PropertyBuilder sourceProperty) { var getSource = ExecBld.GetProp(sourceProperty, ExecBld.GetThis()); foreach (var t in adapterType.GetAllInterfaceProperties()) { var tProp = originalType.GetProperty(t.Name); //, t.PropertyType); var get = ExecBld.Return(ExecBld.GetProp(tProp, getSource)); var set = ExecBld.Return(ExecBld.SetProp(tProp, getSource, ExecBld.GetArg(1))); factory.AddProperty(typeBldr, t, get, set, t.DeclaringType); } }