本文整理汇总了Python中System.Reflection方法的典型用法代码示例。如果您正苦于以下问题:Python System.Reflection方法的具体用法?Python System.Reflection怎么用?Python System.Reflection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System
的用法示例。
在下文中一共展示了System.Reflection方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CreateAssemblyGenerator
# 需要导入模块: import System [as 别名]
# 或者: from System import Reflection [as 别名]
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: test_type_from_reflection_emit
# 需要导入模块: import System [as 别名]
# 或者: from System import Reflection [as 别名]
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()
示例3: test_type_from_reflection_emit
# 需要导入模块: import System [as 别名]
# 或者: from System import Reflection [as 别名]
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()
示例4: DefineType
# 需要导入模块: import System [as 别名]
# 或者: from System import Reflection [as 别名]
def DefineType(self, name):
return TypeGenerator(self, self.mb.DefineType(name, Reflection.TypeAttributes.Public))
示例5: Save
# 需要导入模块: import System [as 别名]
# 或者: from System import Reflection [as 别名]
def Save(self):
self.ab.Save(self.fileName, Reflection.PortableExecutableKinds.ILOnly, Reflection.ImageFileMachine.I386);
示例6: test_pinvoke_method
# 需要导入模块: import System [as 别名]
# 或者: from System import Reflection [as 别名]
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))
示例7: test_assemblybuilder_instance
# 需要导入模块: import System [as 别名]
# 或者: from System import Reflection [as 别名]
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)
示例8: test_type
# 需要导入模块: import System [as 别名]
# 或者: from System import Reflection [as 别名]
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)
示例9: EmitTestMethod
# 需要导入模块: import System [as 别名]
# 或者: from System import Reflection [as 别名]
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)
示例10: test_sanity_override_constructors
# 需要导入模块: import System [as 别名]
# 或者: from System import Reflection [as 别名]
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