Instantiate objects at runtime possible?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have stored a menu in a database. The menu is dynamically updated and last level menu items contain the object type that should be instantiated. I am looking for a way to instantiate the object dynamically without an enormous select case statement. I can't find nothing in the docs... Any ideas?

To make things clear, what I want to do is something like this:

dim objType as new String("CustomerObject")

dim obj as new objType


All instantiated objects have new() constructors.

Mariano.
 
something like

dim objType as Type = Type.GetType("CustomerObject")
dim objConstructor as ConstructorInfo =
objType.GetConstructor(Type.EmptyTypes)
dim obj as Object = objConstructor.Invoke(Nothing)

note: "CustomerObject" must be "full qualified"
so if that object is in nameSpace MyNamespace you should do:
dim objType as Type = Type.GetType("MyNamespace.CustomerObject")


hope this helps

dominique




Mariano said:
Hi,

I have stored a menu in a database. The menu is dynamically updated and
last level menu items contain the object type that should be instantiated. I
am looking for a way to instantiate the object dynamically without an
enormous select case statement. I can't find nothing in the docs... Any
ideas?
 
Take a look at the Activator.CreateInstance:
http://tinyurl.com/29rtl

You could use it like this:
Dim t As Type = Type.GetType("MyNameSpace.MyClass")
Dim o As Object = Activator.CreateInstance(t)

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan


Mariano said:
Hi,

I have stored a menu in a database. The menu is dynamically updated and
last level menu items contain the object type that should be instantiated. I
am looking for a way to instantiate the object dynamically without an
enormous select case statement. I can't find nothing in the docs... Any
ideas?
 
Hi Dominique,

I am curious because an answer from Wim partialy in Dutch, are you a native
Dutch speaker?

Cor
 
yep, from belgium...

wim is also from belgium (his email is same ISP as i have at home ;-))

D.
 
* "=?Utf-8?B?TWFyaWFubw==?= said:
I have stored a menu in a database. The menu is dynamically updated
and last level menu items contain the object type that should be
instantiated. I am looking for a way to instantiate the object
dynamically without an enormous select case statement. I can't find
nothing in the docs... Any ideas?

<http://groups.google.de/[email protected]>
 
Back
Top