Exemplo n.º 1
0
        public static MethodDefinition GetOrCreateStaticConstructor(this TypeDefinition typeDef)
        {
            var cctor = typeDef.Methods.FirstOrDefault(m => m.IsConstructor && m.IsStatic);

            if (cctor == null)
            {
                var module = typeDef.Module;

                cctor = new MethodDefinition(".cctor", MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, module.TypeSystem.Void);
                cctor.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
                cctor.AddAttr <CompilerGeneratedAttribute>(module);

                typeDef.Methods.Add(cctor);
            }

            return(cctor);
        }