Tarik
Tarik

Reputation: 81711

Assembly.ExecutingAssembly() GetType() doesn't return anything

Here is my code, it is very simple:

namespace EnumReflection
{
    enum Messengers
    {
      MSN,
      ICQ,
      YahooChat,
      GoogleTalk
    }

  class Program
  {
    static void Main(string[] args)
    {
      FieldInfo[] fields = typeof(Messengers).GetFields(BindingFlags.Static | BindingFlags.Public);

      foreach (var field in fields)
      {
        Console.WriteLine(field.Name);
      }

      var assembly = Assembly.GetExecutingAssembly();
      var type = assembly.GetType("Messengers");

      Console.ReadLine();
    }
  }
}

However, type variable is always null, even though when I say assembly.GetTypes(), it returns all types such as Messengers, Program.

Here is the copy of my Immediate Window:

assembly.GetType("Messengers");
null
assembly.GetTypes();
{System.Type[2]}
    [0]: {Name = "Messengers" FullName = "EnumReflection.Messengers"}
    [1]: {Name = "Program" FullName = "EnumReflection.Program"}

Upvotes: 0

Views: 173

Answers (2)

ILya
ILya

Reputation: 2778

You should specify a full name in GetType() (i.e. uncluding namespace) in order to get a type

Upvotes: 1

David Yaw
David Yaw

Reputation: 27864

The Assembly.GetType() method is supposed to take the full name of the type, including namespace. Try passing EnumReflection.Messengers instead, that should work.

Upvotes: 4

Related Questions