Example #1
0
        internal MethodILWriter(CILReflectionContextImpl ctx, MetaDataWriter md, CILMethodBase method, EmittingAssemblyMapper mapper)
        {
            var methodIL = (MethodILImpl)method.MethodIL;

            this._method            = method;
            this._methodIL          = methodIL;
            this._metaData          = md;
            this._assemblyMapper    = mapper;
            this._ilCode            = new Byte[methodIL._opCodes.Sum(info => info.MaxSize)];
            this._ilCodeCount       = 0;
            this._methodILOffset    = 0;
            this._opCodeInfoOffsets = new Int32[methodIL._opCodes.Count];
            this._labelInfos        = new LabelEmittingInfo[methodIL._branchTargetsCount];
            this._labelInfoIndex    = 0;

            this._stackSizes   = new Dictionary <Int32, Int32>();
            this._currentStack = 0;
            this._maxStack     = 0;
        }
Example #2
0
        protected CILMethodBaseImpl(
            CILReflectionContextImpl ctx,
            Int32 anID,
            System.Reflection.MethodBase method
            )
            : base(ctx, anID, (method is System.Reflection.ConstructorInfo) ? CILElementKind.Constructor : CILElementKind.Method, () => new CustomAttributeDataEventArgs(ctx, method))
        {
            ArgumentValidator.ValidateNotNull("Method", method);

            if (method.DeclaringType
#if WINDOWS_PHONE_APP
                .GetTypeInfo()
#endif
                .IsGenericType&& !method.DeclaringType
#if WINDOWS_PHONE_APP
                .GetTypeInfo()
#endif
                .IsGenericTypeDefinition)
            {
                throw new ArgumentException("This constructor may be used only on methods declared in genericless types or generic type definitions.");
            }
            if (method is System.Reflection.MethodInfo && method.GetGenericArguments().Any() && !method.IsGenericMethodDefinition)
            {
                throw new ArgumentException("This constructor may be used only on genericless methods or generic method definitions.");
            }

            InitFields(
                ref this.callingConvention,
                ref this.methodAttributes,
                ref this.methodKind,
                ref this.declaringType,
                ref this.parameters,
                ref this.il,
                ref this.methodImplementationAttributes,
                ref this.securityInfo,
                new SettableValueForEnums <CallingConventions>((CallingConventions)method.CallingConvention),
                new SettableValueForEnums <MethodAttributes>((MethodAttributes)method.Attributes),
                this.cilKind == CILElementKind.Constructor ? MethodKind.Constructor : MethodKind.Method,
                () => (CILType)ctx.Cache.GetOrAdd(method.DeclaringType),
                () => ctx.CollectionsFactory.NewListProxy <CILParameter>(method.GetParameters().Select(param => ctx.Cache.GetOrAdd(param)).ToList()),
                () =>
            {
                MethodIL result;
                if (ctx.Cache.ResolveMethodBaseID(this.id).HasILMethodBody())
                {
                    var args = new MethodBodyLoadArgs(method);
                    ctx.LaunchMethodBodyLoadEvent(args);
                    result = new CILAssemblyManipulator.Implementation.Physical.MethodILImpl(this.DeclaringType.Module, args);
                }
                else
                {
                    result = null;
                }
                return(result);
            },
                new SettableLazy <MethodImplAttributes>(() =>
            {
                var args = new MethodImplAttributesEventArgs(method);
                ctx.LaunchMethodImplAttributesEvent(args);
                return(args.MethodImplementationAttributes);
            }),
                new Lazy <DictionaryWithRoles <SecurityAction, ListProxy <SecurityInformation>, ListProxyQuery <SecurityInformation>, ListQuery <SecurityInformation> > >(this.SecurityInfoFromAttributes, LazyThreadSafetyMode.ExecutionAndPublication),
                true
                );
        }