3

尝试覆盖 ToString() 并使用 GetType() 返回正在使用的对象的类型。它正在返回信息,但它包括名称空间。问题是如何剥离 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

4 回答 4

3

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

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) ,它将在编译时将自身替换为包含标准类型名称的常量(以下是下一个示例的反编译):

的名字

例如我有这个代码:

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,版本=1.0.0.0,文化=中性,PublicKeyToken=null

程序

于 2015-10-04T21:21:23.040 回答
2

您正在寻找GetType().Name

于 2015-10-04T21:18:14.530 回答
0

您可以使用 Type.Name 属性 - 请参阅https://msdn.microsoft.com/en-us/library/system.reflection.memberinfo.name(v=vs.110).aspx

于 2015-10-04T21:21:19.603 回答
0

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

var typeName = nameof(Person);

或者正如其他人所说,GetType().Name对于继承自MemberInfo.

无论哪种方式,您还可以使用字符串插值简化连接:

public override string ToString()
{
    return $"{nameof(Person)}:{Number}-{Name}({Age}yo) |
             Total amount is: {AmountDue:C} 
             and the quarterly payment is: { (AmountDue / 4):C}";
}
于 2015-10-04T21:19:46.610 回答