Object.GetType()也返回项目名称

时间:2015-10-04 21:16:38

标签: c# .net gettype nameof

尝试覆盖ToString()并使用GetType()返回正在使用的对象的类型。它正在返回信息,但它包含NameSpace。问题是如何剥离NameSpace并仅显示对象名称。这是我的代码:

namespace Trial
{
   class Testing
   {
      static void Main(string[] args)
      {
         //Variables
         Person[] objectPerson = new Person[5]; //create an array of references
         objectPerson[0] = new Person(10, "John", 35, 1200.00);
         objectPerson[1] = new Person(20, "Jill", 30, 2400.00);
         objectPerson[2] = new Person(30, "Joann", 40, 600.00);
         objectPerson[3] = new Person(40, "Jimmy", 25, 4800.00);
         objectPerson[4] = new Person(50, "Jamie", 45, 300.00);

         for (int y = 0; y < objectPerson.Length; ++y)
         {
            Console.WriteLine(objectPerson[y].ToString());
         }

         Console.Write("\nPress any key to exit . . .");
         Console.ReadKey();
      }
   }
   class Person
   {
      //Data fields
      private int number;
      private string name;
      private int age;
      private double amountDue;

      //Constructors
      public Person(): this(9,"ZZZ",0,0.00)
      {
      }
      public Person(int _Number, string _Name, int _Age, double _AmountDue)
      {
         Number = _Number;
         Name = _Name;
         Age = _Age;
         AmountDue = _AmountDue;
      }
      //Properties
      public int Number
      { 
         get { return number; }
         set { number = value; }
      }
      public string Name
      {
         get { return name; }
         set { name = value; }
      }
      public int Age
      {
         get { return age; }
         set { age = value; }
      }
      public double AmountDue
      {
         get { return amountDue; }
         set { amountDue = value; }
      }

      //Overrides
      public override string ToString()
      {
         return(this.GetType() + ": " + this.Number +  " - " + this.Name + " (" + this.Age.ToString() + "yo) | The total annual ammount is: " + this.AmountDue.ToString("C") + " and the quarterly payment is " + (this.AmountDue / 4).ToString("C"));
      }
   }
}

4 个答案:

答案 0 :(得分:3)

使用它的名称来表示Type的方法很少:

typeof(YourClassName).Name //Output is YourClassName.
typeof(YourClassName).FullName //Output is Namespace + YourClassName.
typeof(YourClassName).AssemblyQualifiedName //Output is Namespace + YourClassName + assembly name, version and signature.

当您打印类型而不是选择其中一种类型时,会调用Type.ToString方法,这会将Type.FullName作为一个隐藏字符串。

同样在C#6.0中,您可以使用语法nameof(YourClassName),它将在编译时用一个常量替换自身,该常量包含该类型的标准名称(以下是来自下一个示例的反编译):

NameOf

例如我有这段代码:

namespace ConsoleTests
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(typeof(Program));
            Console.WriteLine(typeof(Program).Name);
            Console.WriteLine(typeof(Program).FullName);
            Console.WriteLine(typeof(Program).AssemblyQualifiedName);
            Console.WriteLine(nameof(Program));
        }
    }
}

输出是:

  

ConsoleTests.Program

     

程序

     

ConsoleTests.Program

     

ConsoleTests.Program,ConsoleTests,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null

     

程序

答案 1 :(得分:2)

您正在寻找GetType().Name

答案 2 :(得分:0)

如果您使用的是C#-6,则可以使用nameof运算符:

var typeName = nameof(Person);

或正如其他人所说,GetType().Name表示继承自MemberInfo的班级名称。

无论哪种方式,您还可以使用string interpolation简化串联:

public override string ToString()
{
    return $"{nameof(Person)}:{Number}-{Name}({Age}yo) |
             Total amount is: {AmountDue:C} 
             and the quarterly payment is: { (AmountDue / 4):C}";
}

答案 3 :(得分:0)