How to force the class Shared New to execute at start up when classes names are unknown and the

How to force the class SharedNew to execute at start up when classes names are unknown and the application don’t know how many classes there are?

Hi All,

Yesterday I got help to CreateObjectByName. Today my problem is I that don’t know the names of the classes and I don’t know how many classes there are in the application!

(This is also a kind of luxury problem. The work around is that every time I write and add a new particular device class to the application, I manually update the deviceNameList with the name of the device class. But I want this automatically.)

A possible(?) elegant)?) solution may be that each class has a shared new procedure like:

ClassAclass

SharedSubNew()

deviceNameList.Add(deviceTypeShared)

EndSub

…..

End Class

Thereby each particular class puts it name (deviceTypeShared) into the deviceNameList that is later presented to the user in a combo box. The user select a device from the combo box and the device is created withCreateObjectByName.

The problem is that theSharedSubNewis not executed when the application starts up. Only when something in the class is first called. Can I foolSharedSubNewso it executed directly. OR do I need som reflection method like:

For each aClass in Assembly.ClassCollection

If aClass.hasSharedNew then aClass.new

Next

Well, the only problem is: how do I do it?

I hope there is a elegant solution for this problem.

Regards

Csaba

[6075 byte] By [CsabaU] at [2008-1-10]
# 1
You could use Assembly.ExportedTypes to see what types in the assembly are public. Filter them by checking the Type.BaseType property (for example), to check if the type is derived from your base "this is a device" class. Type.GetInterface() would be another approach.
nobugz at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Hi nobugz

Thank you for the answer, I will give it a try and see if I'm able to implement it.

Regards

Csaba

CsabaU at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

Hi nobugz,

I searched on Google witht the "keywords" you gave and combining information from a number of places resulted in this code that works:

Code Snippet

Sub LetDeviceClassesIdentifyThemselvesWithNew()

Try

Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly

For Each thisClass As Type In assembly.GetTypes()

If GetType(SerialDeviceClass).IsAssignableFrom(thisClass) Then

' Shared New and object New will be executed at creation.

Dim device As SerialDeviceClass = CType(assembly.CreateInstance(thisClass.FullName), SerialDeviceClass)

device = Nothing

End If

Next

Catch ex As Exception

g.ExInfo(ex)

End Try

End Sub

PS: My DeviceClass was abstract and it can not be created - it generated an exception!

But the base class SerialDeviceClass works.

Thanks nobugz!

Regards

Csaba

CsabaU at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic General...