DefineDefaultConstructor() public method

public DefineDefaultConstructor ( System attributes ) : System.Reflection.Emit.ConstructorBuilder
attributes System
return System.Reflection.Emit.ConstructorBuilder
Example #1
0
		/// <summary>
		/// Emits constructors.
		/// </summary>
		private void EmitPageConstructors(TypeBuilder type)
		{
			ConstructorBuilder ctor;
			//      ILGenerator il;
			ConstructorInfo ci;

			//public instance void .ctor()
			//{
			//  ldarg.0
			//  call instance void [PhpNetCore]PHP.Core.PhpPage::.ctor()
			//  ret
			//} 

			ctor = type.DefineDefaultConstructor(MethodAttributes.Public);

			ci = typeof(PhpPage).GetConstructor(
			  BindingFlags.NonPublic | BindingFlags.Instance, // flags: instance family    
			  null,                                           // binder
			  new Type[] { },                                  // params
			  null);                                          // modifiers

			/* obsolete
			//public instance void .ctor(class [PhpNetCore]PHP.Core.ConfigurationRecord)
			//{
			//  ldarg.0
			//  ldarg.1
			//  call instance void [PhpNetCore]PHP.Core.PhpPage::.ctor(class [PhpNetCore]PHP.Core.ConfigurationRecord)
			//  ret
			//} 

			ctor = type.DefineConstructor(
			  MethodAttributes.Public,                         // flags
			  CallingConventions.Standard,                     // calling convention
			  new Type[] {typeof(ConfigurationRecord)});       // params
      
			ci = typeof(PhpPage).GetConstructor(
			  BindingFlags.NonPublic | BindingFlags.Instance,  // flags: instance family    
			  null,                                            // binder
			  new Type[] {typeof(ConfigurationRecord)},        // params
			  null);                                           // modifiers
      
			il = ctor.GetILGenerator();

			il.Emit(OpCodes.Ldarg_0);
			il.Emit(OpCodes.Ldarg_1);
			il.Emit(OpCodes.Call,ci);
			il.Emit(OpCodes.Ret);
			*/
		}
Example #2
0
        public static Type CompileResultType(List <FieldInfo> listOfFields, string typeName)
        {
            System.Reflection.Emit.TypeBuilder tb = GetTypeBuilder(typeName);
            ConstructorBuilder constructor        = tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);

            // NOTE: assuming your list contains Field objects with fields FieldName(string) and FieldType(Type)
            foreach (var field in listOfFields)
            {
                CreateProperty(tb, field.Name, field.Type);
            }

            Type objectType = tb.CreateType();

            return(objectType);
        }
Example #3
0
        public Type CompileResultType()
        {
            System.Reflection.Emit.TypeBuilder tb = GetTypeBuilder();
            ConstructorBuilder constructor        =
                tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName |
                                            MethodAttributes.RTSpecialName);

            // NOTE: assuming your list contains Field objects with fields FieldName(string) and FieldType(Type)
            foreach (KeyValuePair <string, Tuple <Type, bool> > field in Fields)
            {
                CreateProperty(tb, field.Key, field.Value.Item1, field.Value.Item2);
            }

            Type objectType = tb.CreateType();

            return(objectType);
        }
Example #4
0
        // Функция предкодогенерационного обхода синтаксического дерева
        //   Нужен для того, чтобы выполнить некоторую подготовительную работу. Например, задекларировать классы
        private void BeforeCompile(Node ActiveNode)
        {
            if (ActiveNode.IsTerminal)
            {
            }
            else
            {
                if (
                    (ActiveNode.TypeNTerminal == NonTerminal.MainClass) ||

                    (ActiveNode.TypeNTerminal == NonTerminal.ClassDecl))
                {
                    if (TypeTable.ContainsKey(ActiveNode.Nodes[1].Value))
                    {
                        throw new System.Exception("Переопределение класса " + ActiveNode.Nodes[1].Value);
                    }
                    else
                    {
                        Emit.TypeBuilder _TypeBuilder =
                            ModBuilder.DefineType(ActiveNode.Nodes[1].Value);

                        TypeTable[ActiveNode.Nodes[1].Value] = _TypeBuilder;

                        if (ActiveNode.TypeNTerminal != NonTerminal.MainClass)
                        {
                            _TypeBuilder.DefineDefaultConstructor(Reflect.MethodAttributes.Public);
                        }
                    }
                }


                foreach (Node Child in ActiveNode.Nodes)
                {
                    BeforeCompile(Child);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Creates a new fluent type builder using the given name, telling if the
        /// generated type will be collectible (if you use the other overloads,
        /// this value is true) and optionally telling which interfaces should be
        /// implemented.
        /// </summary>
        public FluentTypeBuilder(string name, bool isCollectible, params Type[] interfaceTypes)
        {
            _delegatesExpression = () => _delegates;
            _creatorThreadId     = Thread.CurrentThread.ManagedThreadId;
            _isCollectible       = isCollectible;

            Type           baseType;
            HashSet <Type> allInterfaces = new HashSet <Type>();

            if (typeof(TBase).IsInterface)
            {
                baseType = typeof(object);
                allInterfaces.Add(typeof(TBase));
            }
            else
            {
                baseType = typeof(TBase);
            }

            foreach (var interfaceType in typeof(TBase).GetInterfaces())
            {
                allInterfaces.Add(interfaceType);
            }

            if (interfaceTypes != null)
            {
                foreach (var interfaceType in interfaceTypes)
                {
                    if (interfaceType == null)
                    {
                        throw new ArgumentException("interfaceTypes can't contain null values.", "interfaceTypes");
                    }

                    if (!interfaceType.IsInterface)
                    {
                        throw new ArgumentException("interfaceTypes can't contain non-interface types.", "interfaceTypes");
                    }

                    if (!interfaceType.IsVisible)
                    {
                        throw new ArgumentException("interfaceTypes must only contain visible interfaces (public interfaces declared directly in namespaces or inside other public types).", "interfaceTypes");
                    }

                    allInterfaces.Add(interfaceType);
                    foreach (var baseInterface in interfaceType.GetInterfaces())
                    {
                        allInterfaces.Add(baseInterface);
                    }
                }
            }

            AssemblyBuilderAccess access = AssemblyBuilderAccess.Run;

            if (isCollectible)
            {
                access = AssemblyBuilderAccess.RunAndCollect;
            }

            _interfaceTypes = allInterfaces.ToArray();
            var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(name), access);
            var module   = assembly.DefineDynamicModule(name);

            _type = module.DefineType(name, TypeAttributes.NotPublic, baseType, _interfaceTypes);
            _type.DefineDefaultConstructor(MethodAttributes.Public);
            _delegatesField = _type.DefineField(".delegates", typeof(Delegate[]), FieldAttributes.Private | FieldAttributes.Static);
        }
        private static void GenerateConstructor(TypeBuilder dynamicType, string[] properties, List<FieldBuilder> fields)
        {
            const MethodAttributes CtorAttributes = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Public;
            var defaultCtor = dynamicType.DefineDefaultConstructor(CtorAttributes);

            var ctor = dynamicType.DefineConstructor(CtorAttributes, CallingConventions.Standard, fields.Select(x => x.FieldType).ToArray());
            var ctorIl = ctor.GetILGenerator();
            ctorIl.Emit(OpCodes.Ldarg_0);
            ctorIl.Emit(OpCodes.Call, defaultCtor);

            for (var i = 0; i < properties.Length; i++)
            {
                var strParamName = properties[i];
                var field = fields[i];
                var builder3 = ctor.DefineParameter(i + 1, ParameterAttributes.None, strParamName);
                ctorIl.Emit(OpCodes.Ldarg_0);
                ctorIl.Emit(OpCodes.Ldarg, builder3.Position);
                ctorIl.Emit(OpCodes.Stfld, field);
            }

            ctorIl.Emit(OpCodes.Ret);

            AddDebuggerHiddenAttribute(ctor);
        }
Example #7
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 private ConstructorBuilder BuildMethod_ctor(TypeBuilder type)
 {
     System.Reflection.MethodAttributes methodAttributes =
           System.Reflection.MethodAttributes.Public
         | System.Reflection.MethodAttributes.HideBySig;
     return type.DefineDefaultConstructor(methodAttributes);
 }
 /// <summary>
 /// add a no arg constructor, which calls the default constructor of the supertype.
 /// </summary>
 public void AddDefaultConstructor(TypeBuilder builder,
                                   MethodAttributes attrs) {
     builder.DefineDefaultConstructor(attrs);
 }
Example #9
0
		public static ConstructorBuilder DefineDefaultConstructor (TypeBuilder typeBuilder)
		{
			ConstructorBuilder constructorBuilder = typeBuilder.DefineDefaultConstructor (EmitConstants.PUBLIC_CONSTRUCTOR);
			return constructorBuilder;
		}
Example #10
0
 public void BuildMethod_ctor1(TypeBuilder type)
 {
     // Declaring method builder
     // Method attributes
     System.Reflection.MethodAttributes methodAttributes =
           System.Reflection.MethodAttributes.Public
         | System.Reflection.MethodAttributes.HideBySig;
     ctor = type.DefineDefaultConstructor(methodAttributes);
 }
 private static void GenerateWrapperConstructor(TypeBuilder newType, FieldInfo defaultValue)
 {
     ConstructorBuilder ctor = newType.DefineDefaultConstructor(MethodAttributes.Public);
 }
		private static void GenerateConstructor(TypeBuilder type)
		{
			type.DefineDefaultConstructor (MethodAttributes.Public);
		}
 /// <summary>adds an empty default constructor</summary>
 private void DefineEmptyDefaultConstr(TypeBuilder boxBuilder) {
     boxBuilder.DefineDefaultConstructor(MethodAttributes.Public);
 }