1

Here's the code:

public class Program
{
    public static void Main(string[] args)
    {
       Test test = new Test();
    }
}

public class Test
{
    public Test() {
        Console.WriteLine("type: " + Type.GetType("Registration"));        
    }
}

public class Registration
{
    public int mAge;
    public string mName;

    public Registration() {}
}  

When it try to get the type for Registration, it returns null. So both .Name or .AssemblyQualifiedName can't be accessed.

I can provide AssemblyQualifiedName only if GetType() doesn't return null.

Why GetType() returns null? Hope the question is clear.

markzzz
  • 47,390
  • 120
  • 299
  • 507

4 Answers4

7

That works fine. If it doesn't work for you, then there's probably a namespace, in which case you need "TheNamespace.Registration". If the type is in a different assembly, you'll need to specify that too; ultimately the most reliable string version is the type's AssemblyQualifiedName - which could be something like "Registration, ConsoleApp48, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", or longer if there's a strong-name involved.

However, typeof(Registration) would be easier than any of these options.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Why it seems to don't work using different namespace? http://rextester.com/XSIKO23415 – markzzz Mar 16 '18 at 11:05
  • @markzzz the fully qualified name of that (not including the assembly part) is `WebApi.WebAPIController+Registration` (note the `+`, not `.`) – Marc Gravell Mar 16 '18 at 11:13
  • Oh... that's `+`. Why `+` here and not `.`? – markzzz Mar 16 '18 at 11:16
  • 1
    @markzzz because `Type.GetType()` needs to work the same for any caller language, so it uses the CLR's type name conventions, which do not match C#'s type name conventions. In C# a nested type is `Outer.Inner`. In CLR terms, a nested type is `Outer+Inner` – Marc Gravell Mar 16 '18 at 11:27
  • @markzzz similarly, in C# a `List` is: `List`. To the CLR that is `List\`1[[T]]`. As for why, consider: in VB it is `List(Of T)`; in F# it is ... I don't even know what – Marc Gravell Mar 16 '18 at 11:30
  • Thanks dude, clear! +1 – markzzz Mar 16 '18 at 13:02
4

You need to specifiy the "assembly qualified" type name:

Type.GetType("YourNameSpace.Registration");

As the documentation states:

typeName Type: System.String

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.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
3

You need to specify the full name of the type, including the namespace:

Console.WriteLine("type: " + Type.GetType("Rextester.Registration")); 

Or alternatively - depending on what you're trying to do:

Console.WriteLine("type: " + typeof(Registration)); 
stuartd
  • 70,509
  • 14
  • 132
  • 163
0

I think you need to define the full namespace path to your class, so something like TestNamespace.Registration

Steffx115
  • 26
  • 7