How to create a class instance from a string variable

S

Steve Long

Hello,
this is probably fairly simple and I know how to do it in the COM world but
it's escaping me in the .NET world.

I have a string value that contains the name of an object I want to create.
In the COM world, I would just use the CreateObject function passing in the
string value for the ProgID and VB6 would create it. How do I do that for a
..NET type in VB.NET?

VB6 example:
dim s as string
s = "SomeProgID"
dim obj as object

Set obj = CreateObject(s)

The .NET types will not be COM objects.

Your help is much appreciate.
I hope I have been clear here.

Steve
 
C

Charles Law

Hi Steve

Try this

<code>
Dim asmAssemblyContainingClass As [Assembly]
Dim TypeToLoad As Type
Dim obj As Object

asmAssemblyContainingClass = [Assembly].GetAssembly(GetType(..
assemblyWhereClassDefined ..))
TypeToLoad = asmAssemblyContainingClass.GetType("YourClassString")

obj = DirectCast(Activator.CreateInstance(TypeToLoad), Object)
</code>

HTH

Charles
 
S

Steve Long

Charles,
thank you for your quick response. After reading through your code and the
..NET docs, it seems that GetAssembly requires that you know the type that
you will be creating?
I do not know what type I will be creating until runtime. However, I can
assume that I will have the location of the assembly, the name of the
assembly, and the name of the class that is to be created. Is this fair in
the .NET world? I mean can I still do it with the above information?

Steve


Charles Law said:
Hi Steve

Try this

<code>
Dim asmAssemblyContainingClass As [Assembly]
Dim TypeToLoad As Type
Dim obj As Object

asmAssemblyContainingClass = [Assembly].GetAssembly(GetType(..
assemblyWhereClassDefined ..))
TypeToLoad = asmAssemblyContainingClass.GetType("YourClassString")

obj = DirectCast(Activator.CreateInstance(TypeToLoad), Object)
</code>

HTH

Charles


Steve Long said:
Hello,
this is probably fairly simple and I know how to do it in the COM world but
it's escaping me in the .NET world.

I have a string value that contains the name of an object I want to create.
In the COM world, I would just use the CreateObject function passing in the
string value for the ProgID and VB6 would create it. How do I do that
for
a
.NET type in VB.NET?

VB6 example:
dim s as string
s = "SomeProgID"
dim obj as object

Set obj = CreateObject(s)

The .NET types will not be COM objects.

Your help is much appreciate.
I hope I have been clear here.

Steve
 
H

Herfried K. Wagner [MVP]

* "Steve Long said:
I have a string value that contains the name of an object I want to create.

For example (there are many other ways to use 'CreateInstance'):

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByName( _
"System.Windows.Forms", _
"System.Windows.Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add(c)
///
 
S

Steve Long

Hey Charles thanks again. I think I got it now. I can load the assembly as
long as it's either in the currently directory, or in the GAC (if I provide
the version, culture details).

For some reason, Load didn't like it when I supplied the path to the
assembly. Not sure what that's about so if I can't resolve that, it just
means that I'll have to force the developers to install either into the GAC
or into the folder that this particular app lives in. I don't partcularly
like either one of those options so if anyone else knows why I'm having
trouble with the path part of the assembly name let me know.

Steve


Charles Law said:
Hi Steve

Try this

<code>
Dim asmAssemblyContainingClass As [Assembly]
Dim TypeToLoad As Type
Dim obj As Object

asmAssemblyContainingClass = [Assembly].GetAssembly(GetType(..
assemblyWhereClassDefined ..))
TypeToLoad = asmAssemblyContainingClass.GetType("YourClassString")

obj = DirectCast(Activator.CreateInstance(TypeToLoad), Object)
</code>

HTH

Charles


Steve Long said:
Hello,
this is probably fairly simple and I know how to do it in the COM world but
it's escaping me in the .NET world.

I have a string value that contains the name of an object I want to create.
In the COM world, I would just use the CreateObject function passing in the
string value for the ProgID and VB6 would create it. How do I do that
for
a
.NET type in VB.NET?

VB6 example:
dim s as string
s = "SomeProgID"
dim obj as object

Set obj = CreateObject(s)

The .NET types will not be COM objects.

Your help is much appreciate.
I hope I have been clear here.

Steve
 
S

Steve Long

Thanks Herfried. Big help.

Steve

Herfried K. Wagner said:
* "Steve Long said:
I have a string value that contains the name of an object I want to
create.

For example (there are many other ways to use 'CreateInstance'):

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByName( _
"System.Windows.Forms", _
"System.Windows.Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add(c)
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top