コード例 #1
0
def CreateAssemblyGenerator(path, name):
    dir = IO.Path.GetDirectoryName(path)
    file = IO.Path.GetFileName(path)

    aname = Reflection.AssemblyName(name)
    domain = System.AppDomain.CurrentDomain
    ab = domain.DefineDynamicAssembly(aname,
                                      Emit.AssemblyBuilderAccess.RunAndSave,
                                      dir, None)
    mb = ab.DefineDynamicModule(file, file, True)

    ab.DefineVersionInfoResource()

    constructor = clr.GetClrType(
        Diagnostics.DebuggableAttribute).GetConstructor(
            MakeArray(
                System.Type,
                clr.GetClrType(
                    Diagnostics.DebuggableAttribute.DebuggingModes)))
    attributeValue = MakeArray(
        System.Object, Diagnostics.DebuggableAttribute.DebuggingModes.Default
        | Diagnostics.DebuggableAttribute.DebuggingModes.DisableOptimizations
        | Diagnostics.DebuggableAttribute.DebuggingModes.
        IgnoreSymbolStoreSequencePoints)
    cab = Emit.CustomAttributeBuilder(constructor, attributeValue)

    ab.SetCustomAttribute(cab)
    mb.SetCustomAttribute(cab)

    return AssemblyGenerator(file, ab, mb, None)
コード例 #2
0
def test_sanity_static_dot_net_type():
    '''
    Create a new static CLR Type.
    '''
    import clr
    AddReferenceToDlrCore()

    clr.AddReference("IronPythonTest")
    import IronPythonTest.interop.net.type.clrtype as IPT

    from System import Reflection
    from Microsoft.Scripting.Generation import AssemblyGen
    from System.Reflection import Emit, FieldAttributes
    from System.Reflection.Emit import OpCodes
    gen = AssemblyGen(Reflection.AssemblyName('test'), None, '.dll', False)

    class MyType(type):
        def __clrtype__(self):
            baseType = super(MyType, self).__clrtype__()
            t = gen.DefinePublicType(self.__name__, baseType, True)
            ctors = baseType.GetConstructors()
            for ctor in ctors:
                baseParams = ctor.GetParameters()
                newParams = baseParams[1:]
                builder = t.DefineConstructor(
                    Reflection.MethodAttributes.Public,
                    Reflection.CallingConventions.Standard,
                    tuple([p.ParameterType for p in newParams]))
                fldAttrs = FieldAttributes.Static | FieldAttributes.Public
                fld = t.DefineField('$$type', type, fldAttrs)
                ilgen = builder.GetILGenerator()
                ilgen.Emit(OpCodes.Ldarg, 0)
                ilgen.Emit(OpCodes.Ldsfld, fld)
                for index in range(len(ctor.GetParameters())):
                    ilgen.Emit(OpCodes.Ldarg, index + 1)
                ilgen.Emit(OpCodes.Call, ctor)
                ilgen.Emit(OpCodes.Ret)
                # keep a ctor which takes Python types as well so we
                # can be called from Python still.
                builder = t.DefineConstructor(
                    Reflection.MethodAttributes.Public,
                    Reflection.CallingConventions.Standard,
                    tuple([p.ParameterType for p in ctor.GetParameters()]))
                ilgen = builder.GetILGenerator()
                ilgen.Emit(OpCodes.Ldarg, 0)
                for index in range(len(ctor.GetParameters())):
                    ilgen.Emit(OpCodes.Ldarg, index + 1)
                ilgen.Emit(OpCodes.Call, ctor)
                ilgen.Emit(OpCodes.Ret)
            newType = t.CreateType()
            newType.GetField('$$type').SetValue(None, self)
            return newType

    class MyCreatableDotNetType(object):
        __metaclass__ = MyType

        def __init__(self):
            self.abc = 3
コード例 #3
0
    def test_sanity_override_constructors(self):
        '''
        Create a new CLR Type and override all of its constructors.
        '''
        self.add_reference_to_dlr_core()

        from System import Reflection
        from Microsoft.Scripting.Generation import AssemblyGen
        from System.Reflection import Emit, FieldAttributes
        from System.Reflection.Emit import OpCodes
        gen = AssemblyGen(Reflection.AssemblyName('test'), None, '.dll', False)

        try:

            class MyType(type):
                def __clrtype__(self):
                    baseType = super(MyType, self).__clrtype__()
                    t = gen.DefinePublicType(self.__name__, baseType, True)

                    ctors = baseType.GetConstructors()
                    for ctor in ctors:
                        builder = t.DefineConstructor(
                            Reflection.MethodAttributes.Public,
                            Reflection.CallingConventions.Standard,
                            tuple([
                                p.ParameterType for p in ctor.GetParameters()
                            ]))
                        ilgen = builder.GetILGenerator()
                        ilgen.Emit(OpCodes.Ldarg, 0)
                        for index in range(len(ctor.GetParameters())):
                            ilgen.Emit(OpCodes.Ldarg, index + 1)
                        ilgen.Emit(OpCodes.Call, ctor)
                        ilgen.Emit(OpCodes.Ret)

                    newType = t.CreateType()
                    return newType

            class X(object):
                __metaclass__ = MyType

                def __init__(self):
                    self.abc = 3

            a = X()
            self.assertEqual(a.abc, 3)

        finally:
            #gen.SaveAssembly()
            pass
コード例 #4
0
WIN32_PINVOKE_DYNAMIC_ASSEMBLY_NAME = "WIN_API_ASSEMBLY"

PINVOKE_METHOD_ATTRIBUTES = (
        Refl.MethodAttributes.Public |
        Refl.MethodAttributes.Static |
        Refl.MethodAttributes.HideBySig |
        Refl.MethodAttributes.PinvokeImpl
    )

PUBLIC_STATIC_BINDING_FLAGS = Refl.BindingFlags.Static | Refl.BindingFlags.Public

WIN_API_CALLING_CONVENTION = Interop.CallingConvention.StdCall

assemblyBuilder = (
    Emit.AssemblyBuilder.DefineDynamicAssembly(
        Refl.AssemblyName(WIN32_PINVOKE_DYNAMIC_ASSEMBLY_NAME),
        Emit.AssemblyBuilderAccess.Run
      )
  )

MODULE_BUILDER = assemblyBuilder.DefineDynamicModule("WIN_API_MODULE_" + Guid.NewGuid().ToString())

def GetWinApiFunctionImpl(
        functionName,
        moduleName,
        charSet,
        returnType,
        *parameterTypes
    ):
    tbuilder = MODULE_BUILDER.DefineType("WIN_API_TYPE" + "_" + moduleName + "_" + functionName)
    mbuilder = tbuilder.DefinePInvokeMethod(