Ejemplo n.º 1
0
        /// <summary>
        /// <para>Adds a field to TypeBuilder.</para>
        /// <para>Default access level is Public, i.e. field is public and instance level.</para>
        /// <para>
        /// By default, property getters and setters use <c>Public | HideBySig | SpecialName</c>.
        /// For inheritance, use <c>Public | Virtual | NewSlot | SpecialName | HideBySig</c>
        /// </para>
        /// </summary>
        public static PropertyBuilder AddProperty(this TypeBuilder Builder, string MemberName, Type MemberType, MethodAttributes MAttributes = DefaultPropertyAttributes)
        {
            FieldBuilder    backingField = Builder.AddField(BackingFieldNameByConvention(MemberName), MemberType, FieldAttributes.Private);
            PropertyBuilder property     = Builder.DefineProperty(MemberName, PropertyAttributes.HasDefault, MemberType, null);

            MethodBuilder getter   = Builder.DefineMethod($"get_{MemberName}", MAttributes, MemberType, Type.EmptyTypes);
            ILGenerator   getterIL = getter.GetILGenerator();

            getterIL.Emit(OpCodes.Ldarg_0);
            getterIL.Emit(OpCodes.Ldfld, backingField);
            getterIL.Emit(OpCodes.Ret);

            MethodBuilder setter   = Builder.DefineMethod($"set_{MemberName}", MAttributes, null, new Type[] { MemberType });
            ILGenerator   setterIL = setter.GetILGenerator();

            setterIL.Emit(OpCodes.Ldarg_0);
            setterIL.Emit(OpCodes.Ldarg_1);
            setterIL.Emit(OpCodes.Stfld, backingField);
            setterIL.Emit(OpCodes.Ret);

            property.SetGetMethod(getter);
            property.SetSetMethod(setter);

            return(property);
        }
Ejemplo n.º 2
0
        public static FieldBuilder AddField(this TypeBuilder Builder, FieldInfo Field)
        {
            var field = Builder.AddField(Field.Name, Field.MemberType(), Field.Attributes);

            foreach (var attribute in Field.GetCustomAttributesData())
            {
                field.AddAttribute(attribute);
            }

            return(field);
        }
Ejemplo n.º 3
0
        public static FieldBuilder AddProperty <TProperty>(this TypeBuilder typeBuilder, string strPropertyName, bool isNeedGet = true, bool isNeedSet = true)
        {
            strPropertyName = strPropertyName.Trim();
            Type type = typeof(TProperty);

            //FieldBuilder field = typeBuilder.DefineField(strPropertyName.ToLower().Trim(), type, FieldAttributes.Private);
            FieldBuilder field = typeBuilder.AddField <TProperty>(strPropertyName);


            if (isNeedGet || isNeedSet)
            {
                PropertyBuilder  property   = typeBuilder.DefineProperty(strPropertyName, PropertyAttributes.HasDefault, type, null);
                MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

                if (isNeedGet)
                {
                    MethodBuilder getMethod = typeBuilder.DefineMethod($"Get{strPropertyName}", getSetAttr, type, Type.EmptyTypes);
                    ILGenerator   nameGetIL = getMethod.GetILGenerator();
                    nameGetIL.Emit(OpCodes.Ldarg_0);
                    nameGetIL.Emit(OpCodes.Ldfld, field);
                    nameGetIL.Emit(OpCodes.Ret);
                    property.SetGetMethod(getMethod);
                }

                if (isNeedSet)
                {
                    MethodBuilder setMethod = typeBuilder.DefineMethod($"Set{strPropertyName}", getSetAttr, null, new Type[] { type });
                    ILGenerator   nameSetIL = setMethod.GetILGenerator();
                    nameSetIL.Emit(OpCodes.Ldarg_0);
                    nameSetIL.Emit(OpCodes.Ldarg_1);
                    nameSetIL.Emit(OpCodes.Stfld, field);
                    nameSetIL.Emit(OpCodes.Ret);
                    property.SetSetMethod(setMethod);
                }
            }

            return(field);
        }
Ejemplo n.º 4
0
 public static TypeBuilder WithField(this TypeBuilder builder, string name, Type type)
 {
     builder.AddField(name, type);
     return(builder);
 }
Ejemplo n.º 5
0
 public static FieldBuilder AddField <T>(this TypeBuilder Builder, string MemberName, FieldAttributes Attributes = FieldAttributes.Public)
 => Builder.AddField(MemberName, typeof(T), Attributes);
Ejemplo n.º 6
0
 private void AddField(Builder.FieldInfo field)
 {
     _typeBuilder.AddField(field);
 }