A.D.
A.D.

Reputation: 1116

VB Type.GetType("") returns nothing

I have this Enum type:

namespace foo.bar
{
  public enum MyEnum: byte
  {
...
  }
}

When I try to get its type, I use :

Dim t As Type = Type.GetType("foo.bar.MyEnum")

I get t = Nothing???!

Upvotes: 0

Views: 1580

Answers (4)

JohnyL
JohnyL

Reputation: 7152

Adding to other answers, here's another way to achieve what you need.

Say, your C# project has namespace ConsoleNET and produces ConsoleNET.dll. There you defined your enum:

namespace foo.bar
{
    public enum MyEnum : byte { One, Two }
}

Your VB.NET project references this C# project. You can investigate the types in it by loading this library for reflection only:

Sub Main()
    Dim asm = Assembly.ReflectionOnlyLoad("ConsoleNET")
    Dim t = asm.GetType("foo.bar.MyEnum")
    If t Is Nothing Then
        Console.WriteLine("t is nothing")
    Else
        Console.WriteLine("t is not nothing")
    End If
End Sub
'Output: t is not nothing

Upvotes: 1

Brian Cryer
Brian Cryer

Reputation: 2224

Look at Type.GetType("namespace.a.b.ClassName") returns null

According to that Type.GetType(..) only works when the type is found in either mscorlib.dll or the current executing assembly.

So you need to use:

Type.GetType("foo.bar.MyEnum,ClassLibrary1")

replace "ClassLibrary1" with the name of your library that contains the enum and it should then work.

Upvotes: 1

Code Pope
Code Pope

Reputation: 5459

As stated by others you are probably not using the fully qualified type name. I think you are missing the assembly specification. To get the type of you have to give the "assembly-qualified name":

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

Thus in your case it will look something like the following:

Type.GetType("AssemblyName.Namesspace.EnumerationName")

Upvotes: 0

the_lotus
the_lotus

Reputation: 12748

Maybe you are missing the root namespace. Just get the type from the actual enum, this will give you the string value.

GetType(MyEnum).ToString()

From a quick example, I got.

Type.GetType("ConsoleApplication1.Module1+MyEnum").ToString()

Upvotes: 3

Related Questions