Family Tree Mike said:
Activator.CreateInstance is the equivalent to CreateObject in vb6 for .net
classes. The key difference is you have to load the .Net assembly with
Assembly.LoadFrom("somefile.dll"); in your code. There is no magical
lookup akin to COM unless the dll is installed into the GAC.
And add to this the need for an interface ,
while with CreateObject in VB6 you used late binding an you thus only needed
to type the Field / Method names correctly ,
However although i personaly would not recomend it it is possible to use
creatobject through ( just create a COM class ) in .Net , this would also
mean you need to put option strict to Off and you have to go twice to
marshal boundries ,,, so although it is technicly possible it is in my
opinion a no go area ( unless the called dll is a legacy ( VB6 ) dll )
As Mike already told we have now Reflection to do the .Net equivalant of
late binding
To give you an idea ,
In the company i work for we have a production system where all our BL
classes share this interface
'--------------------------------------------------------------------------------------
'---
'--- Purpose : Provide an generic interface for the Queu
'---
'--- Made by : Michel Posseth [MCP]
'--- For : ista netherlands
'--- Date : 20-11-2006
'--- Revissions :
'---
'--- Remarks : Do not break the interface signature !!
'--- you may extend but never remove property`s or methods
'---
'--------------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Public Interface IiQueuObject
Sub StartProcessing()
Property Parameters() As String
Property ProcessId() As String
Event eFinished(ByVal ProcessId As String, ByVal msg As String)
Event eError(ByVal ProcessId As String, ByVal msg As String)
Event eProcessCancelled(ByVal ProcessId As String, ByVal msg As String)
Property CancellProcess() As Boolean
End Interface
A windows service , ( actually a by me written Queue service wich can even
run multiple queues in multiple priorities and orders etc etc etc ) now
queries a database where it only needs to get the path to the dll that
needs to be started
and throw it to this method
'--------------------------------------------------------------------------------------
'---
'--- clsGetObjectFromFile
'---
'--- Purpose : start returns an initiated ista.IiQueuObject
'---
'--- Made by : Michel Posseth [MCP]
'--- For : ista netherlands
'--- Date : 14-11-2006
'--- Revissions : 21-11-2006 : AssPath ingebouwd voor flexibiliteid
'--- 05-12-2007 upgrade naar framework 3.5
'---
'--------------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Imports System.IO
Public Class clsGetObjectFromFile
''' <summary>
''' Laad een object by zijn assembly naam en class naam
''' het te laden object moet een ista.IiQueuObject interface bezitten
''' </summary>
''' <param name="vstrAssemblyName">Name of the VSTR assembly.</param>
''' <param name="vstrClassName">Name of the VSTR class.</param>
''' <returns></returns>
Public Shared Function LoadMeByName(ByVal vstrAssemblyName As String, _
ByVal vstrClassName As String) As ista.IiQueuObject
'<21-11-2006 MP>
If vstrAssemblyName.StartsWith("[AssPath]") Then
Dim appath As String = My.Application.Info.DirectoryPath
vstrAssemblyName = vstrAssemblyName.Replace("[AssPath]", "")
vstrAssemblyName = Path.Combine(appath, vstrAssemblyName)
End If
'</21-11-2006 MP>
Dim objAssembly As Reflection.Assembly
If Not My.Computer.FileSystem.FileExists(vstrAssemblyName) Then
ServMain.WriteLogentry("Assembly niet aanwezig : " & vstrAssemblyName,
EventLogEntryType.Error)
'de assembly is niet aanwezig op deze lokatie
Return Nothing
End If
objAssembly = Reflection.Assembly.LoadFrom(vstrAssemblyName)
'voer een cast uit naar ista.IiQueuObject interface
Try
LoadMeByName = DirectCast(objAssembly.CreateInstance(vstrClassName),
ista.IiQueuObject)
Catch ex As Exception
ServMain.WriteLogentry("Assembly bezit niet de juiste interface : " &
vstrAssemblyName, EventLogEntryType.Error)
Return Nothing
End Try
If LoadMeByName Is Nothing Then
Dim msg As String
msg = "Assembly : " & vstrAssemblyName & Environment.NewLine & _
"Type : " & vstrClassName & " is niet gestart om onduidelijke reden"
ServMain.WriteLogentry(msg, EventLogEntryType.Error)
'geen error gewoon niets terug geven
'wanneer we dus iets anders hebben als een Nothing pointer
'dan hebben we een geinitialiseerd object
Return Nothing
End If
End Function
End Class
I hope this gives you some ideas
regards
Michel