Beispiel #1
0
        public static void AddProperty(this TypeSpecification typeSpec, Action <PropertySpecification> propertySpecExpression)
        {
            var property = new PropertySpecification();

            propertySpecExpression(property);
            typeSpec.Properties.Add(property);
        }
Beispiel #2
0
        public static void AddProperty(
            this TypeBuilder typeBuilder,
            PropertySpecification propertySpec)
        {
            FieldBuilder fieldBuilder = typeBuilder.DefineField("_" + propertySpec.Name.ToLowerInvariant(), propertySpec.Type, FieldAttributes.Private);

            MethodBuilder getterMethodBuilder;

            if (propertySpec.GetterImplementation != null)
            {
                getterMethodBuilder = propertySpec.GetterImplementation(typeBuilder);
            }
            else
            {
                getterMethodBuilder = CreatePropertyGetMethodWithBackingField(
                    typeBuilder, propertySpec.Name, propertySpec.GetterAttributes, fieldBuilder);
            }

            MethodBuilder setterMethodBuilder = CreatePropertySetMethodWithBackingField(
                typeBuilder, propertySpec.Name, propertySpec.SetterAttributes,
                fieldBuilder);

            if (propertySpec.AnnotateAsCompilerGenerated)
            {
                AddCompilerGeneratedAttribute(getterMethodBuilder);
                AddCompilerGeneratedAttribute(setterMethodBuilder);
            }

            if (typeBuilder.BaseType.GetProperty(propertySpec.Name) != null)
            {
                return;
            }

            var property = typeBuilder.DefineProperty(
                propertySpec.Name, PropertyAttributes.None, propertySpec.Type, null);

            property.SetGetMethod(getterMethodBuilder);
            property.SetSetMethod(setterMethodBuilder);

            foreach (var attributeTemplate in propertySpec.Attributes)
            {
                property.AddAttribute(attributeTemplate);
            }
        }
        public static Type CreateTabClass(string className, string tabName, AccessLevel requiredAccess, int sortIndex, TypeAttributes typeAttributes)
        {
            var moduleBuilder = ReflectionExtensions.CreateModuleWithReferenceToPageTypeBuilder("AssemblyWithTab");

            var nameProperty = new PropertySpecification();
            nameProperty.Name = "Name";
            nameProperty.Type = typeof(string);
            nameProperty.GetterImplementation = (typeBuilder) =>
            {
                return typeBuilder.DefineMethodReturningString("get_Name", tabName,
                                                               MethodAttributes.Public | MethodAttributes.Virtual);
            };

            var requiredAccessProperty = new PropertySpecification();
            requiredAccessProperty.Name = "RequiredAccess";
            requiredAccessProperty.Type = typeof(AccessLevel);
            requiredAccessProperty.GetterImplementation = (typeBuilder) =>
            {
                return typeBuilder.DefineMethodReturningEnum("get_RequiredAccess", requiredAccess,
                                                             MethodAttributes.Public | MethodAttributes.Virtual);
            };

            var sortIndexProperty = new PropertySpecification();
            sortIndexProperty.Name = "SortIndex";
            sortIndexProperty.Type = typeof(int);
            sortIndexProperty.GetterImplementation = (typeBuilder) =>
            {
                return typeBuilder.DefineMethodReturningInt("get_SortIndex", sortIndex,
                                                            MethodAttributes.Public | MethodAttributes.Virtual);
            };

            return moduleBuilder.CreateClass(type =>
            {
                type.Name = "NameOfTheTabClass";
                type.ParentType = typeof(Tab);
                type.Properties.Add(nameProperty);
                type.Properties.Add(requiredAccessProperty);
                type.Properties.Add(sortIndexProperty);
                type.TypeAttributes = typeAttributes;
            });
        }
        public static Type CreateTabClass(string className, string tabName, AccessLevel requiredAccess, int sortIndex)
        {
            var moduleBuilder = ReflectionExtensions.CreateModuleWithReferenceToPageTypeBuilder("AssemblyWithTab");

            var nameProperty = new PropertySpecification();
            nameProperty.Name = "Name";
            nameProperty.Type = typeof(string);
            nameProperty.GetterImplementation = (typeBuilder) =>
            {
                MethodBuilder getMethodBuilder =
                    typeBuilder.DefineMethod("get_Name", MethodAttributes.Public | MethodAttributes.Virtual, typeof(string), Type.EmptyTypes);

                ILGenerator ilGenerator = getMethodBuilder.GetILGenerator();
                ilGenerator.Emit(OpCodes.Ldstr, tabName);
                ilGenerator.Emit(OpCodes.Ret);
                return getMethodBuilder;
            };

            var requiredAccessProperty = new PropertySpecification();
            requiredAccessProperty.Name = "RequiredAccess";
            requiredAccessProperty.Type = typeof(AccessLevel);
            requiredAccessProperty.GetterImplementation = (typeBuilder) =>
            {
                MethodBuilder getMethodBuilder =
                    typeBuilder.DefineMethod("get_RequiredAccess", MethodAttributes.Public | MethodAttributes.Virtual, typeof(AccessLevel), Type.EmptyTypes);

                ILGenerator ilGenerator = getMethodBuilder.GetILGenerator();
                ilGenerator.DeclareLocal(typeof(AccessLevel));
                ilGenerator.Emit(OpCodes.Ldc_I4, (int)requiredAccess);
                ilGenerator.Emit(OpCodes.Stloc_0);
                ilGenerator.Emit(OpCodes.Ldloc_0);
                ilGenerator.Emit(OpCodes.Ret);
                return getMethodBuilder;
            };

            var sortIndexProperty = new PropertySpecification();
            sortIndexProperty.Name = "SortIndex";
            sortIndexProperty.Type = typeof(int);
            sortIndexProperty.GetterImplementation = (typeBuilder) =>
            {
                MethodBuilder getMethodBuilder =
                    typeBuilder.DefineMethod("get_SortIndex", MethodAttributes.Public | MethodAttributes.Virtual, typeof(int), Type.EmptyTypes);

                ILGenerator ilGenerator = getMethodBuilder.GetILGenerator();
                ilGenerator.DeclareLocal(typeof(int));
                ilGenerator.Emit(OpCodes.Ldc_I4, sortIndex);
                ilGenerator.Emit(OpCodes.Stloc_0);
                ilGenerator.Emit(OpCodes.Ldloc_0);
                ilGenerator.Emit(OpCodes.Ret);
                return getMethodBuilder;
            };

            return moduleBuilder.CreateClass(type =>
            {
                type.Name = "NameOfTheTabClass";
                type.ParentType = typeof(Tab);
                type.Properties.Add(nameProperty);
                type.Properties.Add(requiredAccessProperty);
                type.Properties.Add(sortIndexProperty);
            });
        }
 public static void AddProperty(this TypeSpecification typeSpec, Action<PropertySpecification> propertySpecExpression)
 {
     var property = new PropertySpecification();
     propertySpecExpression(property);
     typeSpec.Properties.Add(property);
 }
        public static void AddProperty(
            this TypeBuilder typeBuilder,
            PropertySpecification propertySpec)
        {
            

            FieldBuilder fieldBuilder = typeBuilder.DefineField("_" + propertySpec.Name.ToLowerInvariant(), propertySpec.Type, FieldAttributes.Private);

            MethodBuilder getterMethodBuilder;
            if (propertySpec.GetterImplementation != null)
            {
                getterMethodBuilder = propertySpec.GetterImplementation(typeBuilder);
            }
            else
            {
                getterMethodBuilder = CreatePropertyGetMethodWithBackingField(
                    typeBuilder, propertySpec.Name, propertySpec.GetterAttributes, fieldBuilder);
            }

            MethodBuilder setterMethodBuilder = CreatePropertySetMethodWithBackingField(
                typeBuilder, propertySpec.Name, propertySpec.SetterAttributes, 
                fieldBuilder);

            if (propertySpec.AnnotateAsCompilerGenerated)
            {
                AddCompilerGeneratedAttribute(getterMethodBuilder);
                AddCompilerGeneratedAttribute(setterMethodBuilder);
            }

            if (typeBuilder.BaseType.GetProperty(propertySpec.Name) != null)
                return;

            var property = typeBuilder.DefineProperty(
                propertySpec.Name, PropertyAttributes.None, propertySpec.Type, null);

            property.SetGetMethod(getterMethodBuilder);
            property.SetSetMethod(setterMethodBuilder);

            foreach (var attributeTemplate in propertySpec.Attributes)
            {
                property.AddAttribute(attributeTemplate);
            }
        }