Dynamic Class Object

ddelella

New member
Joined
Jul 20, 2009
Messages
2
Programming Experience
5-10
Does anyone know how to create an object of a dynamic class? For example: The user selects a project from a list of projects. Each project has its own class and object of the format <projectid>.<projectid>_obj. When I change the drop down of the project I want the study to reinstantiate an object called my study as a type of that class object. So if the study was ABC123 it would be:

Dim myStudy As New ABC123.ABC123_obj

If I changed the drop down to XYZ123 it would reload the object as:

myStudy = New XYZ123.XYZ123_obj

Does anyone know if this is possible? The idea is to avoid creating multiple instances of the same application which only differ in this one line.
 
Not Dynamic

The problem with interfaces is that they are not dynamic. You have to specify the "Implements" portion of the original class for the interace to work. In my case only one of the environment would be using the interface so the classes would not work when they are moved out to production. The method I am hoping for is completely dynamic kind of like an eval statement except for a class name and not a variable name.
 
The only thing which comes right now to my mind is turning Option Strict Off, using an Object and praying that you don't hit the floor.

Or you write a wrapper for each class...though, not very dynamic.

May I ask what this is for?
 
It sounds like you are looking to add a plugin framework to your application. Are the "projects" that you are talking about actually .NET projects or are they compiled class libraries (dll's)? I am not sure how I would handle .NET projects, but if you are using DLL's you could look into using the Reflection library. This will allow you to find known classes in a class library and instantiate them as needed.

Hope this helps!
 
The objective is a little bit vague, but it sounds like the Class definition for these objects are in some class library, but you don't necessarily know the what's going to be in the class library at compile time of the calling program? But presumably there is some set method names that are common across all of these objects, so you know what method to call once you've got the object you want?


One approach would be to implement some Factory object in the Class library dll, and then use call by name.

Like
Public Class MyFactory
Shared function GetMyObject(name as string) as object
Select Case Name
Case "Foo"
return new foo
Case "Bar"
return new Bar
Case "Goo"
return new Goo
Select
End Function
End Class

Then in your client side code you have something like

Function GetStudy(name as string) as object
dim myfact as new MyFactory
return CallByName(myfact, name, CallType.Get)
End Function

Is that the kind of thing you are talking about?
 
Back
Top