Esempio n. 1
0
        /// <summary>
        /// Gets a method emitter for the static constructor of the type.
        /// </summary>
        /// <returns></returns>
        public MethodEmitter GetStaticConstructor()
        {
            if (staticConstructor == null)
            {
                var method = Target.Methods.Where(m => m.IsConstructor && m.IsStatic).FirstOrDefault();

                if (method != null)
                {
                    staticConstructor = new MethodEmitter(this, method);
                }
                else
                {
                    method = new MethodDefinition(".cctor", MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.Private, Module.TypeSystem.Void);

                    if (Target.IsGenericInstance)
                    {
                        foreach (var generic in Target.GenericParameters)
                        {
                            method.GenericParameters.Add(generic);
                        }
                    }

                    Target.Methods.Add(method);

                    staticConstructor = new MethodEmitter(this, method);
                    staticConstructor.GetIL().Emit(Codes.Return);
                }

                var code = staticConstructor.GetIL();
                code.Insert   = CodeInsertion.Before;
                code.Position = code.GetLast();
            }

            return(staticConstructor);
        }