C# 命名空间导致NullReferenceException?

C# 命名空间导致NullReferenceException?,c#,oop,exception,namespaces,C#,Oop,Exception,Namespaces,我有以下程序,这是一段示例代码,显示了C#reflection如何对类进行操作。一切正常,一点问题也没有 public class Program { public static void Main() { Type T = Type.GetType("Customer"); Console.WriteLine("Information about the Type object: "); Console.WriteLine(T.Name); Console.

我有以下程序,这是一段示例代码,显示了C#reflection如何对类进行操作。一切正常,一点问题也没有

public class Program
{

public static void Main()
{

    Type T = Type.GetType("Customer");
    Console.WriteLine("Information about the Type object: ");
    Console.WriteLine(T.Name);
    Console.WriteLine(T.FullName);
    Console.WriteLine();

    Console.WriteLine("Property info:");
    PropertyInfo[] myPropertyInfoArray = T.GetProperties();

    foreach(PropertyInfo myProperty in myPropertyInfoArray)
    {
        Console.WriteLine(myProperty.PropertyType.Name);
    }
    Console.WriteLine();

    Console.WriteLine("Methods in Customer:");
    MethodInfo[] myMethodInfoArray = T.GetMethods();

    foreach(MethodInfo myMethod in myMethodInfoArray)
    {
        Console.WriteLine(myMethod.Name);
    }


    Console.ReadKey();
}

}


class Customer
{

public int ID {get;set;}
public string Name {get;set;}

public Customer()
{
    this.ID = -1;
    this.Name = string.Empty;
}

public Customer(int ID, string Name)
{
    this.ID = ID;
    this.Name = Name;
}

public void PrintID()
{
    Console.WriteLine("ID: {0}", this.ID);
}

public void PrintName()
{
    Console.WriteLine("Name: {0}", this.Name);
}

}

我遇到的问题是,当我将所有代码包装到一个名称空间中时,我突然在类型对象上得到一个NullReferenceException。为什么会这样?

因为它不再知道客户在哪里。你需要

Type T = Type.GetType("NameSpaceName.Customer");

因为它不再知道客户在哪里。你需要

Type T = Type.GetType("NameSpaceName.Customer");

具体在哪一行?您的名称空间名称是什么?具体在哪一行?您的命名空间名称是什么?对,因为GetType不知道有关命名空间的任何信息。对,因为GetType不知道有关命名空间的任何信息