Python System.Reflection() Examples
The following are 18
code examples of System.Reflection().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
System
, or try the search function
.
Example #1
Source File: test_reachtype.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_type_from_reflection_emit(self): import clr import System if is_netcoreapp: clr.AddReference("System.Reflection.Emit") sr = System.Reflection sre = System.Reflection.Emit array = System.Array cab = array[sre.CustomAttributeBuilder]([sre.CustomAttributeBuilder(clr.GetClrType(System.Security.SecurityTransparentAttribute).GetConstructor(System.Type.EmptyTypes), array[object]([]))]) if is_netcoreapp: # no System.AppDomain.DefineDynamicAssembly ab = sre.AssemblyBuilder.DefineDynamicAssembly(sr.AssemblyName("temp"), sre.AssemblyBuilderAccess.Run, cab) # tracking: 291888 else: ab = System.AppDomain.CurrentDomain.DefineDynamicAssembly(sr.AssemblyName("temp"), sre.AssemblyBuilderAccess.RunAndSave, "temp", None, None, None, None, True, cab) # tracking: 291888 mb = ab.DefineDynamicModule("temp", "temp.dll") tb = mb.DefineType("EmittedNS.EmittedType", sr.TypeAttributes.Public) tb.CreateType() clr.AddReference(ab) import EmittedNS EmittedNS.EmittedType()
Example #2
Source File: test_missing.py From ironpython2 with Apache License 2.0 | 6 votes |
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)
Example #3
Source File: test_missing.py From ironpython3 with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: test_reachtype.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_type_from_reflection_emit(self): import clr import System if is_netcoreapp: clr.AddReference("System.Reflection.Emit") sr = System.Reflection sre = System.Reflection.Emit array = System.Array cab = array[sre.CustomAttributeBuilder]([sre.CustomAttributeBuilder(clr.GetClrType(System.Security.SecurityTransparentAttribute).GetConstructor(System.Type.EmptyTypes), array[object]([]))]) if is_netcoreapp: # no System.AppDomain.DefineDynamicAssembly ab = sre.AssemblyBuilder.DefineDynamicAssembly(sr.AssemblyName("temp"), sre.AssemblyBuilderAccess.Run, cab) # tracking: 291888 else: ab = System.AppDomain.CurrentDomain.DefineDynamicAssembly(sr.AssemblyName("temp"), sre.AssemblyBuilderAccess.RunAndSave, "temp", None, None, None, None, True, cab) # tracking: 291888 mb = ab.DefineDynamicModule("temp", "temp.dll") tb = mb.DefineType("EmittedNS.EmittedType", sr.TypeAttributes.Public) tb.CreateType() clr.AddReference(ab) import EmittedNS EmittedNS.EmittedType()
Example #5
Source File: test_missing.py From ironpython3 with Apache License 2.0 | 5 votes |
def DefineType(self, name): return TypeGenerator(self, self.mb.DefineType(name, Reflection.TypeAttributes.Public))
Example #6
Source File: test_assembly.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_type(self): from System.Reflection import Assembly from System.Reflection.Emit import AssemblyBuilder mscorlib = Assembly.Load("mscorlib") self.assertTrue("Assembly" in repr(mscorlib)) self.assertGreaterEqual(len(dir(Assembly)), 75) self.assertGreaterEqual(len(dir(AssemblyBuilder)), 89)
Example #7
Source File: test_assembly.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_assemblybuilder_instance(self): import System name = System.Reflection.AssemblyName() name.Name = 'Test' assemblyBuilder = System.AppDomain.CurrentDomain.DefineDynamicAssembly(name, System.Reflection.Emit.AssemblyBuilderAccess.Run) asm_builder_dir = dir(assemblyBuilder) self.assertGreaterEqual(len(asm_builder_dir), 89) self.assertTrue("GetCustomAttributesData" in asm_builder_dir) self.assertTrue("AddResourceFile" in asm_builder_dir) self.assertTrue("CreateInstance" in asm_builder_dir)
Example #8
Source File: test_clrtype.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_pinvoke_method(self): self.assertTrue(NativeMethods.IsCharAlpha('A')) # Call statically-typed method from another .NET language (simulated using Reflection) isCharAlpha = clr.GetClrType(NativeMethods).GetMethod('IsCharAlpha') args = System.Array[object](('1'.Chars[0],)) self.assertFalse(isCharAlpha.Invoke(None, args))
Example #9
Source File: test_missing.py From ironpython3 with Apache License 2.0 | 5 votes |
def Save(self): self.ab.Save(self.fileName, Reflection.PortableExecutableKinds.ILOnly, Reflection.ImageFileMachine.I386);
Example #10
Source File: test_missing.py From ironpython2 with Apache License 2.0 | 5 votes |
def DefineType(self, name): return TypeGenerator(self, self.mb.DefineType(name, Reflection.TypeAttributes.Public))
Example #11
Source File: test_assembly.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_type(self): from System.Reflection import Assembly from System.Reflection.Emit import AssemblyBuilder mscorlib = Assembly.Load("mscorlib") self.assertTrue("Assembly" in repr(mscorlib)) self.assertGreaterEqual(len(dir(Assembly)), 75) self.assertGreaterEqual(len(dir(AssemblyBuilder)), 89)
Example #12
Source File: test_assembly.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_assemblybuilder_instance(self): import System name = System.Reflection.AssemblyName() name.Name = 'Test' assemblyBuilder = System.AppDomain.CurrentDomain.DefineDynamicAssembly(name, System.Reflection.Emit.AssemblyBuilderAccess.Run) asm_builder_dir = dir(assemblyBuilder) self.assertGreaterEqual(len(asm_builder_dir), 89) self.assertTrue("GetCustomAttributesData" in asm_builder_dir) self.assertTrue("AddResourceFile" in asm_builder_dir) self.assertTrue("CreateInstance" in asm_builder_dir)
Example #13
Source File: test_clrtype.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_pinvoke_method(self): self.assertTrue(NativeMethods.IsCharAlpha('A')) # Call statically-typed method from another .NET language (simulated using Reflection) isCharAlpha = clr.GetClrType(NativeMethods).GetMethod('IsCharAlpha') args = System.Array[object](('1'.Chars[0],)) self.assertFalse(isCharAlpha.Invoke(None, args))
Example #14
Source File: test_missing.py From ironpython2 with Apache License 2.0 | 5 votes |
def Save(self): self.ab.Save(self.fileName, Reflection.PortableExecutableKinds.ILOnly, Reflection.ImageFileMachine.I386);
Example #15
Source File: test___clrtype__.py From ironpython2 with Apache License 2.0 | 4 votes |
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
Example #16
Source File: test_missing.py From ironpython3 with Apache License 2.0 | 4 votes |
def EmitTestMethod(tg, name, argType): cg = tg.DefineMethod(name, Reflection.MethodAttributes.Public | Reflection.MethodAttributes.Static, str, argType) pb = cg.mb.DefineParameter(1, Reflection.ParameterAttributes.In | Reflection.ParameterAttributes.Optional, "o"); tostring = cg.ilg.DeclareLocal(str) baseType = argType if baseType.IsByRef: baseType = baseType.GetElementType() if not baseType.IsValueType: is_null = cg.ilg.DefineLabel() end = cg.ilg.DefineLabel() EmitArg(cg, argType) cg.ilg.Emit(OpCodes.Brfalse_S, is_null) cg.ilg.BeginExceptionBlock() EmitArg(cg, argType) cg.ilg.EmitCall(OpCodes.Callvirt, clr.GetClrType(object).GetMethod("ToString"), None) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.BeginCatchBlock(clr.GetClrType(System.Exception)) cg.ilg.Emit(OpCodes.Callvirt, clr.GetClrType(System.Exception).GetMethod("get_Message")) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.Emit(OpCodes.Ldstr, "#EXCEPTION#") cg.ilg.Emit(OpCodes.Ldloc, tostring) cg.ilg.EmitCall(OpCodes.Call, clr.GetClrType(str).GetMethod("Concat", MakeArray(System.Type, clr.GetClrType(System.String), clr.GetClrType(System.String))), None) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.EndExceptionBlock() cg.ilg.Emit(OpCodes.Br_S, end); cg.ilg.MarkLabel(is_null) cg.ilg.Emit(OpCodes.Ldstr, "(null)") cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.MarkLabel(end) else: cg.ilg.BeginExceptionBlock() EmitArg(cg, argType) cg.ilg.Emit(OpCodes.Box, baseType); cg.ilg.EmitCall(OpCodes.Callvirt, clr.GetClrType(object).GetMethod("ToString"), None) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.BeginCatchBlock(clr.GetClrType(System.Exception)) cg.ilg.Emit(OpCodes.Callvirt, clr.GetClrType(System.Exception).GetMethod("get_Message")) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.Emit(OpCodes.Ldstr, "#EXCEPTION#") cg.ilg.Emit(OpCodes.Ldloc, tostring) cg.ilg.EmitCall(OpCodes.Call, clr.GetClrType(System.String).GetMethod("Concat", MakeArray(System.Type, clr.GetClrType(System.String), clr.GetClrType(System.String))), None) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.EndExceptionBlock() cg.ilg.Emit(OpCodes.Ldstr, name + "$" + argType.FullName + "$") cg.ilg.Emit(OpCodes.Ldloc, tostring) cg.ilg.EmitCall(OpCodes.Call, clr.GetClrType(System.String).GetMethod("Concat", MakeArray(System.Type, clr.GetClrType(System.String), clr.GetClrType(System.String))), None) if trace: cg.ilg.Emit(OpCodes.Dup) cg.ilg.EmitCall(OpCodes.Call, clr.GetClrType(System.Console).GetMethod("WriteLine", MakeArray(System.Type, clr.GetClrType(System.String))), None) cg.ilg.Emit(OpCodes.Ret)
Example #17
Source File: test_missing.py From ironpython2 with Apache License 2.0 | 4 votes |
def EmitTestMethod(tg, name, argType): cg = tg.DefineMethod(name, Reflection.MethodAttributes.Public | Reflection.MethodAttributes.Static, str, argType) pb = cg.mb.DefineParameter(1, Reflection.ParameterAttributes.In | Reflection.ParameterAttributes.Optional, "o"); tostring = cg.ilg.DeclareLocal(str) baseType = argType if baseType.IsByRef: baseType = baseType.GetElementType() if not baseType.IsValueType: is_null = cg.ilg.DefineLabel() end = cg.ilg.DefineLabel() EmitArg(cg, argType) cg.ilg.Emit(OpCodes.Brfalse_S, is_null) cg.ilg.BeginExceptionBlock() EmitArg(cg, argType) cg.ilg.EmitCall(OpCodes.Callvirt, clr.GetClrType(object).GetMethod("ToString"), None) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.BeginCatchBlock(clr.GetClrType(System.Exception)) cg.ilg.Emit(OpCodes.Callvirt, clr.GetClrType(System.Exception).GetMethod("get_Message")) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.Emit(OpCodes.Ldstr, "#EXCEPTION#") cg.ilg.Emit(OpCodes.Ldloc, tostring) cg.ilg.EmitCall(OpCodes.Call, clr.GetClrType(str).GetMethod("Concat", MakeArray(System.Type, clr.GetClrType(System.String), clr.GetClrType(System.String))), None) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.EndExceptionBlock() cg.ilg.Emit(OpCodes.Br_S, end); cg.ilg.MarkLabel(is_null) cg.ilg.Emit(OpCodes.Ldstr, "(null)") cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.MarkLabel(end) else: cg.ilg.BeginExceptionBlock() EmitArg(cg, argType) cg.ilg.Emit(OpCodes.Box, baseType); cg.ilg.EmitCall(OpCodes.Callvirt, clr.GetClrType(object).GetMethod("ToString"), None) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.BeginCatchBlock(clr.GetClrType(System.Exception)) cg.ilg.Emit(OpCodes.Callvirt, clr.GetClrType(System.Exception).GetMethod("get_Message")) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.Emit(OpCodes.Ldstr, "#EXCEPTION#") cg.ilg.Emit(OpCodes.Ldloc, tostring) cg.ilg.EmitCall(OpCodes.Call, clr.GetClrType(System.String).GetMethod("Concat", MakeArray(System.Type, clr.GetClrType(System.String), clr.GetClrType(System.String))), None) cg.ilg.Emit(OpCodes.Stloc, tostring) cg.ilg.EndExceptionBlock() cg.ilg.Emit(OpCodes.Ldstr, name + "$" + argType.FullName + "$") cg.ilg.Emit(OpCodes.Ldloc, tostring) cg.ilg.EmitCall(OpCodes.Call, clr.GetClrType(System.String).GetMethod("Concat", MakeArray(System.Type, clr.GetClrType(System.String), clr.GetClrType(System.String))), None) if trace: cg.ilg.Emit(OpCodes.Dup) cg.ilg.EmitCall(OpCodes.Call, clr.GetClrType(System.Console).GetMethod("WriteLine", MakeArray(System.Type, clr.GetClrType(System.String))), None) cg.ilg.Emit(OpCodes.Ret)
Example #18
Source File: test___clrtype.py From ironpython3 with Apache License 2.0 | 4 votes |
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