Marius S.
Marius S.

Reputation: 559

vb.net Getting a reference to a class by its name

I'm trying to use reflection to get the instance of a class in vb.net. I have a class 'A' in my web project and to test it, i create a new aspx page and try to write the following:

Dim t as Type = Type.GetType("A")

This returns "Nothing". But if i do this:

Dim inst as A = new A()
Dim t as Type = inst.GetType()

t's type is "A"

So how come i can't get the type with GetType even if the name is exactly the same? It does works for things like System.Math though so i'm probably missing something as a newbie.

Upvotes: 0

Views: 1806

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503924

Two things:

  • You need to include the namespace of the type
  • If the type isn't in mscorlib or the currently executing assembly, you need to specify the assembly name as well (including version numbers and public key information if it's a strongly-named assembly).

So for instance, to get hold of System.Linq.Enumerable you'd need something like:

Type.GetType("System.Linq.Enumerable, System.Core, Version=4.0.0.0, " & _
             "Culture=neutral, PublicKeyToken=b77a5c561934e089")

Upvotes: 3

Related Questions