Skip to content

without duck typing

rollynoel edited this page Jun 18, 2013 · 3 revisions

Added by Rodrigo B. de Oliveira,

import System.Threading
import System.Reflection

def CreateInstance(progid):
	type = System.Type.GetTypeFromProgID(progid)
	return type()

def SetProperty(target, name, value):
	target.GetType().InvokeMember(name, BindingFlags.SetProperty, null, target, (value,))

def GetProperty(target, name):
	return target.GetType().InvokeMember(name, BindingFlags.GetProperty, null, target, null)

def Invoke(target, name, arg):
	return target.GetType().InvokeMember(name, BindingFlags.InvokeMethod, null, target, (arg,))

ie = CreateInstance("InternetExplorer.Application")
SetProperty(ie, "Visible", true)
Invoke(ie, "Navigate2", "http://www.go-mono.com/monologue/")

Thread.Sleep(50ms) while GetProperty(ie, "Busy")

document = GetProperty(ie, "Document")
print("$(GetProperty(document, 'title')) is $(GetProperty(document, 'fileSize')) bytes long.")

Alternatively, one could use the tlbimp utility included with the .net framework sdk to generate an interop assembly from the Internet Explorer type library.

Clone this wiki locally