Ejemplo n.º 1
0
        def __clrtype__(self):
            global called

            baseType = super(MyType, self).__clrtype__()
            typegen = Snippets.Shared.DefineType(
                "faux type property" + str(TYPE_COUNTER), baseType, True,
                False)
            typebld = typegen.TypeBuilder

            for ctor in baseType.GetConstructors():
                ctorparams = ctor.GetParameters()
                ctorbld = typebld.DefineConstructor(
                    ctor.Attributes, ctor.CallingConvention,
                    tuple([p.ParameterType for p in ctorparams]))
                ilgen = ctorbld.GetILGenerator()
                ilgen.Emit(OpCodes.Ldarg, 0)
                for index in range(len(ctorparams)):
                    ilgen.Emit(OpCodes.Ldarg, index + 1)
                ilgen.Emit(OpCodes.Call, ctor)
                ilgen.Emit(OpCodes.Ret)

            #Add the property
            prop_type = clr.GetClrType(System.UInt64)
            field_builder = typebld.DefineField("NOT_SO_DYNAMIC", prop_type,
                                                FieldAttributes.Public)
            prop_method_attribs = (MethodAttributes.Public
                                   | MethodAttributes.SpecialName
                                   | MethodAttributes.HideBySig)
            #Property getter
            prop_getter_builder = typebld.DefineMethod("get_NOT_SO_DYNAMIC",
                                                       prop_method_attribs,
                                                       prop_type, None)
            getilgen = prop_getter_builder.GetILGenerator()
            getilgen.Emit(OpCodes.Ldarg_0)
            getilgen.Emit(OpCodes.Ldfld, field_builder)
            getilgen.Emit(OpCodes.Ret)
            #Propery setter
            prop_setter_builder = typebld.DefineMethod("set_NOT_SO_DYNAMIC",
                                                       prop_method_attribs,
                                                       None, (prop_type, ))
            setilgen = prop_setter_builder.GetILGenerator()
            setilgen.Emit(OpCodes.Ldarg_0)
            setilgen.Emit(OpCodes.Ldarg_1)
            setilgen.Emit(OpCodes.Stfld, field_builder)
            setilgen.Emit(OpCodes.Ret)
            #Actual property
            prop_builder = typebld.DefineProperty("NOT_SO_DYNAMIC",
                                                  PropertyAttributes.None,
                                                  prop_type, None)
            prop_builder.SetGetMethod(prop_getter_builder)
            prop_builder.SetSetMethod(prop_setter_builder)

            #Hook the C# property up to the Python version
            new_type = typebld.CreateType()
            fldinfo = new_type.GetField("NOT_SO_DYNAMIC")
            setattr(self, "NOT_SO_DYNAMIC", ReflectedField(fldinfo))

            called = True
            return new_type
Ejemplo n.º 2
0
 def map_fields(self, new_type):
     if hasattr(self, "_clrfields"):
         for fldname in self._clrfields: 
             fldinfo = new_type.GetField(fldname)
             setattr(self, fldname, ReflectedField(fldinfo))