DefineTypeInitializer() public method

public DefineTypeInitializer ( ) : System.Reflection.Emit.ConstructorBuilder
return System.Reflection.Emit.ConstructorBuilder
        protected override void OnPropertyGenerationComplete(TypeDefinition proxyType)
        {
            const string initPropertyWrappersMethodName = "InitPropertyWrappers";

            var initPropertyWrappersMethod = proxyType.DefineMethod(
                initPropertyWrappersMethodName,
                MethodAttributes.Static | MethodAttributes.Private,
                null, Type.EmptyTypes);

            var il = initPropertyWrappersMethod.GetILGenerator();

            this.initPropertyWrapperIlAction.ForEach(m => m(il));
            il.Emit(OpCodes.Ret);

            var cctor = proxyType.DefineTypeInitializer();

            var cctorIl = cctor.GetILGenerator();

            cctorIl.Emit(OpCodes.Call, initPropertyWrappersMethod);
            cctorIl.Emit(OpCodes.Ret);
        }
Esempio n. 2
0
        /// <summary>
        /// Ensure the call sites container is created and return the <see cref="TypeBuilder"/>.
        /// </summary>
        /// <returns></returns>
        private TypeBuilder/*!*/EnsureContainer()
        {
            if (containerClass == null)
            {
                if (this.classContext != null && this.classContext.IsGeneric)
                {
                    // we will emit single call sites in the class context. It is easier than to build generic sites container.
                    Debug.Assert(this.classContext.RealTypeBuilder != null);
                    return this.classContext.RealTypeBuilder;
                }

                Debug.Assert(staticCtorEmitter == null);

                var containerClassName = string.Format("<{0}>o_Sitescontainer'{1}", this.userFriendlyName.Replace('.', '_'), System.Threading.Interlocked.Increment(ref nextContainerId));
                containerClass = moduleBuilder.DefineType(PluginHandler.ConvertCallSiteName(containerClassName), TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.NotPublic | TypeAttributes.Abstract);

                staticCtorEmitter = new ILEmitter(containerClass.DefineTypeInitializer());
            }

            return containerClass;
        }
        // Emits a constructor that takes an object that the surrogate wraps, as well as 
        // a type initializer that will load (for this AppDomain) all the PropertyDescriptors 
        // for virtual properties.
        private static void EmitConstructorsAndProperties(TypeBuilder typeBuilder, Type type, Type parentSurrogateType, Type parentType, FieldInfo wrapperField)
        {
            // public Surrogate(Entity entity) : base(entity)
            ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { type });
            ILGenerator constructorGenerator = constructorBuilder.GetILGenerator();

            if (parentSurrogateType == typeof(object))
            {
                // base()
                constructorGenerator.Emit(OpCodes.Ldarg_0);
                constructorGenerator.Emit(OpCodes.Call, parentSurrogateType.GetConstructor(Type.EmptyTypes));
                
                // _wrapper = wrapper
                constructorGenerator.Emit(OpCodes.Ldarg_0);
                constructorGenerator.Emit(OpCodes.Ldarg_1);
                constructorGenerator.Emit(OpCodes.Stfld, wrapperField);
            }
            else
            {
                // base(entity)
                constructorGenerator.Emit(OpCodes.Ldarg_0);
                constructorGenerator.Emit(OpCodes.Ldarg_1);
                constructorGenerator.Emit(OpCodes.Call, parentSurrogateType.GetConstructor(new Type[] { parentType }));
            }

            constructorGenerator.Emit(OpCodes.Ret);

            // Only generate a type initializer if there are any virtual properties.
            Lazy<ILGenerator> typeInitializerFactory = new Lazy<ILGenerator>(() =>
            {
                // static Surrogate() {
                //     PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Entity));
                //     (_Property = properties["Property"];)*
                // }
                ConstructorBuilder typeInitializerBuilder = typeBuilder.DefineTypeInitializer();
                ILGenerator typeInitializerGenerator = typeInitializerBuilder.GetILGenerator();

                // PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Entity));
                typeInitializerGenerator.DeclareLocal(typeof(PropertyDescriptorCollection));

                typeInitializerGenerator.Emit(OpCodes.Ldtoken, type);
                typeInitializerGenerator.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle"));
                typeInitializerGenerator.Emit(OpCodes.Call, typeof(TypeDescriptor).GetMethod("GetProperties", new Type[] { typeof(Type) }));
                typeInitializerGenerator.Emit(OpCodes.Stloc_0);
                return typeInitializerGenerator;
            });

            // Generate all the properties from here, because properties potentially add code to the type initializer.
            EmitProperties(typeBuilder, type, parentSurrogateType, wrapperField, typeInitializerFactory);

            if (typeInitializerFactory.IsValueCreated)
            {
                typeInitializerFactory.Value.Emit(OpCodes.Ret);
            }
        }
Esempio n. 4
0
 private void CreateStaticConstructor(TypeBuilder typeBuilder, RppField instanceField, RppClass obj)
 {
     FieldInfo instanceFieldInfo = instanceField.FieldInfo.Native;
     ConstructorBuilder staticConstructor = typeBuilder.DefineTypeInitializer();
     _body = staticConstructor.GetILGenerator();
     instanceField.InitExpr.Accept(this);
     _body.Emit(OpCodes.Stsfld, instanceFieldInfo);
     _body.Emit(OpCodes.Ret);
 }
        protected override void OnPropertyGenerationComplete(TypeDefinition proxyType)
        {
            const string initPropertyWrappersMethodName = "InitPropertyWrappers";

            var initPropertyWrappersMethod = proxyType.DefineMethod(
                initPropertyWrappersMethodName,
                MethodAttributes.Static | MethodAttributes.Private,
                null, Type.EmptyTypes);

            var il = initPropertyWrappersMethod.GetILGenerator();
            this.initPropertyWrapperIlAction.ForEach(m => m(il));
            il.Emit(OpCodes.Ret);

            var cctor = proxyType.DefineTypeInitializer();

            var cctorIl = cctor.GetILGenerator();
            cctorIl.Emit(OpCodes.Call, initPropertyWrappersMethod);
            cctorIl.Emit(OpCodes.Ret);
        }
        private Type DeserializeCore(TypeBuilder tb)
        {
            m_currentTb = tb;
            // TODO custom attr for tb
            ReadCustomAttributes(tb);

            foreach (var t in ReadTypeArray())
                tb.AddInterfaceImplementation(t);

            int num = m_br.ReadInt32();
            for (int i = 0; i < num; i++)
            {
                var fb = tb.DefineField(
                    m_br.ReadString(),
                    ReadType(),
                    (FieldAttributes)m_br.ReadUInt32());

                // TODO custom attr for fb
                //Console.WriteLine($"Read => {fb.Name} = {fb.MetadataToken:X8}");

                m_fields.Add(m_br.ReadString(), fb);
            }

            num = m_br.ReadInt32();
            for (int i = 0; i < num; i++)
                ReadMethod();

            // read props
            num = m_br.ReadInt32();
            for (int i = 0; i < num; i++)
            {
                var pb = tb.DefineProperty(
                    m_br.ReadString(),
                    (PropertyAttributes)m_br.ReadUInt32(),
                    ReadType(),
                    ReadTypeArray());

                // TODO custom attr for pb

                if (m_br.ReadBoolean())
                {
                    // default value
                }

                if (m_br.ReadBoolean()) // canread
                    pb.SetGetMethod(ReadMethod());
                if (m_br.ReadBoolean()) // canwrite
                    pb.SetSetMethod(ReadMethod());

            }

            num = m_br.ReadInt32();
            for (int i = 0; i < num; i++)
            {
                var cb = tb.DefineConstructor(
                    (MethodAttributes)m_br.ReadUInt32(),
                    (CallingConventions)m_br.ReadUInt32(),
                    ReadTypeArray());

                ReadConstructor(cb);
            }

            if (m_br.ReadBoolean())
            {
                var ti = tb.DefineTypeInitializer();
                ReadConstructor(ti, true);
            }

            num = m_br.ReadInt32();
            for (int i = 0; i < num; i++)
            {
                var mb = ResolveMethod(m_br.ReadString());
                if (!mb.IsAbstract)
                    ReadMethodBody(mb);
            }

            num = m_br.ReadInt32();
            for (int i = 0; i < num; i++)
                DeserializeNested(tb);

            return tb.CreateType();
        }