示例#1
0
        /// <summary>
        /// Adds property wrapper for the field.
        /// </summary>
        /// <param name="dynamicType">Dynamic type instance</param>
        /// <param name="name">Name of the property</param>
        /// <returns>Dynamic property instance</returns>
        public DynamicProperty ConvertToProperty(DynamicType dynamicType, string name)
        {
            DynamicProperty dynamicProperty = dynamicType.AddProperty(
                name,
                this.FieldBuilder.FieldType,
                PropertyAttributes.HasDefault
            );

            dynamicProperty.CreateGetMethod(dynamicType, this);
            dynamicProperty.CreateSetMethod(dynamicType, this);

            return dynamicProperty;
        }
示例#2
0
        /// <summary>
        /// Creates a setter method which sets specified field.
        /// </summary>
        /// <param name="dynamicType">Dynamic type instance</param>
        /// <param name="dynamicField">Dynamic field instance</param>
        /// <param name="methodAttributes">Method attributes</param>
        /// <returns>Dynamic method instance</returns>
        public OwnDynamicMethod CreateSetMethod(DynamicType dynamicType, DynamicField dynamicField, MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig)
        {
            OwnDynamicMethod dynamicMethod = dynamicType.AddMethod(
                "set_" + this.PropertyBuilder.Name,
                null,
                new Type[] { this.PropertyBuilder.PropertyType },
                methodAttributes
            );

            ILGenerator setMethodILGenerator = dynamicMethod.MethodBuilder.GetILGenerator();
            setMethodILGenerator.Emit(OpCodes.Ldarg_0);
            setMethodILGenerator.Emit(OpCodes.Ldarg_1);
            setMethodILGenerator.Emit(OpCodes.Stfld, dynamicField.FieldBuilder);

            setMethodILGenerator.Emit(OpCodes.Ret);

            this.PropertyBuilder.SetSetMethod(dynamicMethod.MethodBuilder);

            return dynamicMethod;
        }