Пример #1
0
        protected override void Implement(DynamicTypeBuilder config, TypeBuilder typeBuilder, ILGenerator il)
        {
            var properties = config.DynamicMembers.OfType <DynamicProperty>().Where(p => p.Property.CanWrite);

            Label endLabel = il.DefineLabel();

            if (properties.Any())
            {
                Dictionary <DynamicProperty, Label> propLabels = GetSetValueHelper.CreateMethodBody(
                    properties,
                    il,
                    ref endLabel);

                foreach (var item in propLabels)
                {
                    il.MarkLabel(item.Value);
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldarg_2);
                    if (!item.Key.MemberType.IsValueType)
                    {
                        il.Emit(OpCodes.Castclass, item.Key.MemberType);
                    }
                    else
                    {
                        il.Emit(OpCodes.Unbox_Any, item.Key.MemberType);
                    }

                    il.EmitCall(OpCodes.Callvirt, item.Key.Property.GetSetMethod(), null);
                    il.Emit(OpCodes.Nop);
                    il.Emit(OpCodes.Br, endLabel);
                }
            }
            else
            {
                il.Emit(OpCodes.Br_S, endLabel);
            }

            il.MarkLabel(endLabel);
            il.Emit(OpCodes.Ret);
        }
Пример #2
0
        protected override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder, ILGenerator il)
        {
            Label endLabel = il.DefineLabel();

            Dictionary <DynamicProperty, Label> propLabels = GetSetValueHelper.CreateMethodBody(config.DynamicMembers.OfType <DynamicProperty>(), il, ref endLabel);

            foreach (var item in propLabels)
            {
                il.MarkLabel(item.Value);
                il.Emit(OpCodes.Ldarg_0);
                il.EmitCall(OpCodes.Callvirt, item.Key.Property.GetGetMethod(), null);
                if (item.Key.MemberType.IsValueType)
                {
                    il.Emit(OpCodes.Box, item.Key.MemberType);
                }

                il.Emit(OpCodes.Ret);
            }

            il.MarkLabel(endLabel);
            il.Emit(OpCodes.Ldnull);
            il.Emit(OpCodes.Ret);
        }