Esempio n. 1
0
        /// <summary>
        /// This method generates properties and fields
        /// </summary>
        /// <param name="tb">Represents the new type</param>
        /// <param name="properties">A list of <see cref="DynamicProperty"/></param>
        /// <returns>A list of <see cref="FieldInfo"/></returns>
        public FieldInfo[] GenerateProperties(TypeBuilder tb, DynamicProperty[] properties)
        {
            FieldInfo[] fields = new FieldBuilder[properties.Length];

            for (int i = 0; i < properties.Length; i++)
            {
                DynamicProperty dp = properties[i];

                // Field
                FieldBuilder fb = tb.DefineField($"<{properties[i].Name}>i__Field", dp.Type, FieldAttributes.Private);

                // Property
                PropertyBuilder pb = tb.DefineProperty(dp.Name, PropertyAttributes.HasDefault, dp.Type, null);

                // Get()
                MethodBuilder getter = tb.DefineMethod($"get_{dp.Name}", MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, CallingConventions.HasThis, dp.Type, Type.EmptyTypes);

                ILGenerator genGetter = getter.GetILGenerator();
                genGetter.Emit(OpCodes.Ldarg_0);   // This
                genGetter.Emit(OpCodes.Ldfld, fb); // Field.Value
                genGetter.Emit(OpCodes.Ret);       //This.Field

                pb.SetGetMethod(getter);

                // Set()
                MethodBuilder mbSet = tb.DefineMethod($"set_{dp.Name}", MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, CallingConventions.HasThis, null, new Type[] { dp.Type });

                mbSet.DefineParameter(1, ParameterAttributes.In, dp.Name);

                ILGenerator genSet = mbSet.GetILGenerator();
                genSet.Emit(OpCodes.Ldarg_0);   //This
                genSet.Emit(OpCodes.Ldarg_1);   //Value
                genSet.Emit(OpCodes.Stfld, fb); //This.Field = value
                genSet.Emit(OpCodes.Ret);       //Void

                pb.SetSetMethod(mbSet);

                fields[i] = fb;
            }

            return(fields);
        }
Esempio n. 2
0
        public FieldInfo[] GenerateProperties(TypeBuilder tb, DynamicProperty[] properties)
        {
            FieldInfo[] fields = new FieldBuilder[properties.Length];
            for (int i = 0; i < properties.Length; i++)
            {
                DynamicProperty dp = properties[i];
                FieldBuilder    fb = tb.DefineField("_" + dp.Name, dp.Type, FieldAttributes.Private);
                PropertyBuilder pb = tb.DefineProperty(dp.Name, PropertyAttributes.HasDefault, dp.Type, null);

                MethodBuilder mbGet = tb.DefineMethod("get_" + dp.Name,
                                                      MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
                                                      dp.Type, Type.EmptyTypes);

                ILGenerator genGet = mbGet.GetILGenerator();
                genGet.Emit(OpCodes.Ldarg_0);
                genGet.Emit(OpCodes.Ldfld, fb);
                genGet.Emit(OpCodes.Ret);

                MethodBuilder mbSet = tb.DefineMethod("set_" + dp.Name,
                                                      MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig,
                                                      null, new Type[] { dp.Type });


                mbSet.DefineParameter(1, ParameterAttributes.In, dp.Name);

                ILGenerator genSet = mbSet.GetILGenerator();
                genSet.Emit(OpCodes.Ldarg_0);
                genSet.Emit(OpCodes.Ldarg_1);
                genSet.Emit(OpCodes.Stfld, fb);
                genSet.Emit(OpCodes.Ret);

                pb.SetGetMethod(mbGet);
                pb.SetSetMethod(mbSet);

                fields[i] = fb;
            }
            return(fields);
        }