I was curious about the methods exposed when you do an import clr
command. The source code for ClrModule.cs is here. Methods marked as “Advanced” are highly specialized and will not be covered.
Methods
AddReference(string[] references) : void
Add DLL to internal list of assemblies useable by code. Adds a reference to a .NET assembly. Parameters can be an already loaded Assembly object, a full assembly name, or a partial assembly name. After the load the assemblies namespaces and top-level types will be available via import Namespace.
import clr import System clr.AddReference('System.Core')
AddReferenceByName(string[] names) : void
Adds a reference to a .NET assembly. Parameters are an assembly name. After the load the assemblies namespaces and top-level types will be available via import Namespace.
AddReferenceByPartialName(string[] names) : void
Adds a reference to a .NET assembly. Parameters are a partial assembly name. After the load the assemblies namespaces and top-level types will be available via import Namespace.
AddReferenceToFile(string[] files) : void
Adds a reference to a .NET assembly. One or more assembly names can be provided. The assembly is searched for in the directories specified in sys.path
and dependencies will be loaded from sys.path
as well. The assembly name should be the filename on disk without a directory specifier and optionally including the .EXE or .DLL extension. After the load the assemblies namespaces and top-level types will be available via import Namespace.
AddReferenceToFileAndPath(string[] files) : void
Adds a reference to a .NET assembly. One or more assembly names can be provided which are fully qualified names to the file on disk. The directory is added to sys.path and AddReferenceToFile is then called. After the load the assemblies namespaces and top-level types will be available via import Namespace.
AddReferenceToTypeLibrary(object rcw) : void
Advanced. Makes the type lib desc available for importing. See also LoadTypeLibrary.
AddReferenceToTypeLibrary(Guid typeLibGuid) : void
Advanced. Makes the type lib desc available for importing. See also LoadTypeLibrary.
ArgChecker()
Advanced.
ClearProfilerData()
Advanced.
CompileModules()
Advanced.
CompileSubclassTypes()
Advanced.
Convert(object o, Type toType) : object
Attempts to convert the provided object to the specified type. Conversions that will be attempted include standard Python conversions as well as .NET implicit and explicit conversions.
If the conversion cannot be performed a TypeError will be raised.
Deserialize(string serializationFormat, string data) : object
Deserializes the result of a Serialize call. This can be used to perform serialization for .NET types which are serializable. This method is the callable object provided from reduce_ex for .serializable .NET types.
The first parameter indicates the serialization format and is the first tuple element returned from the Serialize call.
The second parameter is the serialized data.
Dir() : List
Returns the result of dir(o)
as-if “import clr” has not been performed. Might use different sort order than DirClr().
DirClr() : List
Returns the result of dir(o)
as-if “import clr” has been performed. Might use different sort order than Dir().
EnableProfiler()
Advanced.
GetClrType(Type type) : Type
Gets the CLR Type object from a given Python type object.
GetProfilerData()
Advanced.
GetPythonType(Type t) : PythonType
Gets the Python type object from a given CLR Type object.
GetSubclassedTypes()
Advanced.
ImportExtensions(NamespaceTracker @namespace) : void
Load .NET extension library.
# Add LINQ support. import clr import System clr.AddReference('System.Core') clr.ImportExtensions(System.Linq) acctMgr = args.Context.GetService(IAccountsManager) ta = (IAccountsManager.TrustAccounts.GetValue(acctMgr) .Where(lambda t: t.Code == args.Context.EmailSubjectLine and t.Enabled == True) .FirstOrDefault())
LoadAssemblyByName(string name) : Assembly
Loads an assembly from the specified assembly name and returns the assembly object. Namespaces or types in the assembly can be accessed directly from the assembly object.
LoadAssemblyByPartialName(string name) : Assembly
Loads an assembly from the specified partial assembly name and returns the assembly object. Namespaces or types in the assembly can be accessed directly from the assembly object.
LoadAssemblyFromFile(string name) : Assembly
Loads an assembly from the specified filename and returns the assembly object. Namespaces or types in the assembly can be accessed directly from the assembly object.
LoadAssemblyFromFileWithPath(string file) : Assembly
Adds a reference to a .NET assembly. Parameters are a full path to an assembly on disk. After the load the assemblies namespaces and top-level types will be available via import Namespace.
LoadTypeLibrary(Guid typeLibGuid) : ComTypeLibInfo
Reads the latest registered type library for the corresponding GUID, reads definitions of CoClass’es and Enum’s from this library and creates a IDynamicMetaObjectProvider that allows to instantiate coclasses and get actual values for the enums.
LoadTypeLibrary(object rcw) : ComTypeLibInfo
Gets an ITypeLib object from OLE Automation compatible RCW, reads definitions of CoClass’es and Enum’s from this library and creates an object that allows to instantiate coclasses and get actual values for the enums.
Reference()
Advanced.
References()
Advanced.
sealed class ReferencesList()
Advanced.
ReturnChecker()
Advanced.
RuntimeArgChecker()
Advanced.
RuntimeReturnChecker()
Advanced.
Self() : object
Returns None.
Serialize(): PythonTuple
Serializes data using the .NET serialization formatter for complex types. Returns a tuple identifying the serialization format and the serialized data which can be fed back into clr.Deserialize.
Current serialization formats include custom formats for primitive .NET types which aren’t already recognized as tuples. None is used to indicate that the Binary .NET formatter is used.
SetCommandDispatcher()
Advanced.
StrongBox()
Advanced.
Use(string name) : object
Attempts to load the specified module searching all languages in the loaded ScriptRuntime.
Use(string path, string language) : object
Attempts to load the specified module belonging to a specific language loaded into the current ScriptRuntime.
__name__
Value of clr.__name__
is “clr”.
accepts()
Advanced.
returns
Advanced.