1

Since IronPython doesn't support attributes I am wondering if there is another way to decorate IronPython classes with attributes, perhaps with reflection?

andersjanmyr
  • 11,302
  • 5
  • 29
  • 29

2 Answers2

2

I'm not sure if this is what you are looking for, but clrtype allows you to use attribute.

import clr
import clrtype
from System.Runtime.InteropServices import DllImportAttribute
import System

class PInvoke(object):

    __metaclass__ = clrtype.ClrClass
    DllImport = clrtype.attribute(DllImportAttribute)

    @staticmethod
    @DllImport("user32.dll")
    @clrtype.accepts(System.UInt32)
    @clrtype.returns(System.Boolean)
    def MessageBeep(beepType): raise RuntimeError("Something went wrong.")

PInvoke.MessageBeep(0)

I'm not sure if it works on classes though.

jcao219
  • 2,808
  • 3
  • 21
  • 22
  • 5
    In IryonPython 2.6.2 and 2.7 Beta 2, I don't find that clrtype module. Can you give me a hint where it is? – MarkusSchaber Mar 18 '11 at 11:58
  • Can you help me please convert for example this: `[DllImport("user32.dll")] static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo)` To IronPython that i can understand how it work? – Dmitry Dronov Sep 09 '21 at 19:59
2

One, albeit ugly and sometimes impractical, workaround is to create a stub class in C# and decorate it with attributes and derive from that in IronPython.