Esempio n. 1
0
        public ILEmitter GetILEmitter()
        {
            if (_emitterField != null)
            {
                return(_emitterField);
            }

            if (DynamicTypeInfo == null)
            {
                return(new ReflectionILEmitter(AsDynamicMethod.GetILGenerator()));
            }

            if (MethodBuilder != null)
            {
                return(new ReflectionILEmitter(MethodBuilder.GetILGenerator()));
            }
            var parameterTypes = _parametersField.Select(p => p.Type)
                                 .ToArray();

            MethodBuilder = DynamicTypeInfo.TypeBuilder.DefineMethod(
                MethodName,
                _methodAttributesField,
                CallingConventions.HasThis,
                ReturnType,
                parameterTypes);

            var ilgen = MethodBuilder.GetILGenerator();

            foreach (var variable in Variables)
            {
                ilgen.DeclareLocal(variable.Type);
            }
            return(new ReflectionILEmitter(MethodBuilder.GetILGenerator()));
        }
Esempio n. 2
0
        public ILEmitter GetILEmitter()
        {
            if (_emitter != null)
            {
                return(_emitter);
            }

            if (DynamicTypeInfo == null)
            {
                return(new ReflectionILEmitter(AsDynamicMethod.GetILGenerator()));
            }

            if (MethodBuilder != null)
            {
                return(new ReflectionILEmitter(MethodBuilder.GetILGenerator()));
            }
            var parameterTypes = _parametersField.Select(p => p.Type)
                                 .ToArray();

            if ((_methodAttributes & MethodAttributes.Static) == MethodAttributes.Static)
            {
                MethodBuilder = DynamicTypeInfo.TypeBuilder.DefineMethod(
                    MethodName,
                    _methodAttributes,
                    ReturnType,
                    parameterTypes);
            }
            else
            {
                MethodBuilder = DynamicTypeInfo.TypeBuilder.DefineMethod(
                    RealMethodName,
                    _methodAttributes,
                    CallingConventions.HasThis,
                    ReturnType,
                    parameterTypes);

                if (ExplImplementedInterface != null)
                {
                    var interfaceMethod = ExplImplementedInterface.GetMethod(MethodName);
                    DynamicTypeInfo.TypeBuilder.DefineMethodOverride(MethodBuilder, interfaceMethod);
                }
            }

            var ilgen = MethodBuilder.GetILGenerator();

            foreach (var variable in Variables)
            {
                ilgen.DeclareLocal(variable.Type);
            }
            return(new ReflectionILEmitter(MethodBuilder.GetILGenerator()));
        }