public void GenerateStaticParts(TypeBuilder typeBuilder) { _name = $"{_opCode}_{(object)_methodInfo ?? _constructorInfo}"; Type[] paramTypesWithoutResult; if (_methodInfo != null) { var isFunction = _methodInfo.ReturnType != typeof(void); var genDelType = GetDelegateType(isFunction, (_methodInfo.IsStatic ? 0 : 1) + _methodInfo.GetParameters().Length); var paramTypes = new List <Type>(); if (!_methodInfo.IsStatic) { paramTypes.Add(_methodInfo.DeclaringType); } paramTypes.AddRange(_methodInfo.GetParameters().Select(p => p.ParameterType)); paramTypesWithoutResult = paramTypes.ToArray(); if (isFunction) { paramTypes.Add(_methodInfo.ReturnType); } _delegateType = genDelType.MakeGenericType(paramTypes.ToArray()); _fieldBuilder = typeBuilder.DefineField("_" + _name, _delegateType, FieldAttributes.Private | FieldAttributes.Static); _staticMethod = typeBuilder.DefineMethod(_name, MethodAttributes.Private | MethodAttributes.Static, _methodInfo.ReturnType, paramTypesWithoutResult); } else if (_constructorInfo != null) { var genDelType = GetDelegateType(true, _constructorInfo.GetParameters().Length); var paramTypes = new List <Type>(); paramTypes.AddRange(_constructorInfo.GetParameters().Select(p => p.ParameterType)); paramTypesWithoutResult = paramTypes.ToArray(); var constructorType = _constructorInfo.DeclaringType; var accesibleConstructorType = IsPublicReal(constructorType) ? constructorType : typeof(object); paramTypes.Add(accesibleConstructorType); _delegateType = genDelType.MakeGenericType(paramTypes.ToArray()); _fieldBuilder = typeBuilder.DefineField("_" + _name, _delegateType, FieldAttributes.Private | FieldAttributes.Static); _staticMethod = typeBuilder.DefineMethod(_name, MethodAttributes.Private | MethodAttributes.Static, accesibleConstructorType, paramTypesWithoutResult); } else { throw new InvalidOperationException(); } var ilGen = new ILGenImpl(_staticMethod.GetILGenerator(), new ILGenForbidenInstructionsGodPowers()); ilGen.Ldsfld(_fieldBuilder); _parametersCount = paramTypesWithoutResult.Length; for (ushort i = 0; i < _parametersCount; i++) { ilGen.Ldarg(i); } ilGen.Call(_delegateType.GetMethod("Invoke")); ilGen.Ret(); }