Пример #1
0
        private static PropertyBuilder CreateProperty(this TypeBuilder typeBuilder, EasyProperty property)
        {
            if (typeBuilder == null)
            {
                throw new ArgumentNullException(nameof(typeBuilder), "Type builder can't be null.");
            }

            MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

            var fieldName = "_" + property.Name.Substring(0, 1).ToLowerInvariant() + property.Name.Substring(1);

            FieldBuilder    fieldBuilder    = typeBuilder.DefineField(fieldName, property.Type, FieldAttributes.Private);
            PropertyBuilder propertyBuilder =
                typeBuilder.DefineProperty(property.Name, PropertyAttributes.HasDefault, property.Type, null);

            MethodBuilder getterBuilder =
                typeBuilder.DefineMethod($"get_{property.Name}", getSetAttr, property.Type, Type.EmptyTypes);

            LoadIlToGetter(getterBuilder, fieldBuilder);

            MethodBuilder setterBuilder =
                typeBuilder.DefineMethod($"set_{property.Name}", getSetAttr, null, new[] { property.Type });

            LoadIlToSetter(setterBuilder, fieldBuilder);

            propertyBuilder.SetGetMethod(getterBuilder);
            propertyBuilder.SetSetMethod(setterBuilder);

            if (property.AttributeType != null)
            {
                var attrBuilder = GetAttributeBuilder(property.AttributeType, property.AttributeCtorParams);
                propertyBuilder.SetCustomAttribute(attrBuilder);
            }

            return(propertyBuilder);

            #region Local Functions

            void LoadIlToGetter(MethodBuilder getterBldr, FieldBuilder fieldBldr)
            {
                ILGenerator getterGenerator = getterBldr.GetILGenerator();

                getterGenerator.Emit(OpCodes.Ldarg_0);
                getterGenerator.Emit(OpCodes.Ldfld, fieldBldr);
                getterGenerator.Emit(OpCodes.Ret);
            }

            void LoadIlToSetter(MethodBuilder setterBldr, FieldBuilder fieldBldr)
            {
                ILGenerator setterGenerator = setterBldr.GetILGenerator();

                setterGenerator.Emit(OpCodes.Ldarg_0);
                setterGenerator.Emit(OpCodes.Ldarg_1);
                setterGenerator.Emit(OpCodes.Stfld, fieldBldr);
                setterGenerator.Emit(OpCodes.Ret);
            }

            #endregion
        }
Пример #2
0
        public static Type CreateType(string typeName, EasyProperty property)
        {
            var typeBuilder = GetTypeBuilder($"Assembly{_next}", $"Module{_next}", typeName);

            typeBuilder.CreateProperty(property);
            ++_next;

            return(typeBuilder.CreateType());
        }
Пример #3
0
        public static Type CreateType(string typeName, string assemblyName, string moduleName, EasyProperty property)
        {
            var typeBuilder = GetTypeBuilder(assemblyName, moduleName, typeName);

            typeBuilder.CreateProperty(property);
            ++_next;

            return(typeBuilder.CreateType());
        }