2015-10-04 78 views
3

嘗試覆蓋ToString()並使用GetType()返回正在使用的對象的類型。它正在返回信息,但它包含NameSpace。問題是如何剝離NameSpace並僅顯示對象名稱。這裏是我的代碼:Object.GetType()也返回項目名稱

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")); 
     } 
    } 
} 
+0

你可以使用'nameof(Person)'。 –

回答

1

您正在尋找GetType().Name

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}"; 
} 
+0

爲什麼不是'{AmountDue:C}'而不是'{AmountDue.ToString(「C」)}'? – PetSerAl

+0

@PetSerAI你說得對。固定。 –

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方法爲i相反,它給你Type.FullName作爲一個deafult字符串。

在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,版本= 1.0.0.0,文化=中立,公鑰=空

計劃

+0

'wait or ways ..?' – MethodMan

+0

哈哈,輸入的很快,在編輯中改變了,謝謝! –