Type.GetType("ClassName") returns null

65 Views Asked by At

this works:

Type type = typeof(ClassName);

these four do not work, the type is null:

string classeName = "ClassName";
string classeNameWithNamenspace = "namespace.ClassName";
Assemly assembly = Assembly.Load("MyAssemblyName");

Type type = Type.GetType($"{ClassName}");
Type type = Type.GetType($"{classeNameWithNamenspace}");

Type type = Type.GetType($"{classeName}, {assembly.FullName}");
Type type = Type.GetType($"{classeNameWithNamenspace}, {assembly.FullName}");

Tested all the variants described above. Checked that all names are spelled correctly. Also checked with Debug.

I don't know what to do, it should actually work.

1

There are 1 best solutions below

0
Paul Sinnema On

The types for all GetType() are returned by this code. What is it you want to achieve?

using System.Reflection;

internal class Program
{
    private static void Main(string[] args)
    {
        string classeName = nameof(TestClass);
        var classeNameWithNamenspace = typeof(TestClass).AssemblyQualifiedName;
        //var assembly = Assembly.Load(classeNameWithNamenspace);

        Type? type1 = Type.GetType(classeName);
        Type? type2 = Type.GetType(classeNameWithNamenspace);

        //Type? type3 = Type.GetType($"{classeName}, {assembly.FullName}");
        //Type? type4 = Type.GetType($"{classeNameWithNamenspace}, {assembly.FullName}");
    }
}

public class TestClass
{
}