Object.GetType() 也返回项目名称
Object.GetType() also returning project name
尝试覆盖 ToString() 并使用 GetType() 来 return 正在使用的对象的类型。它正在 returning 信息,但它包括 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"));
}
}
}
您正在寻找 GetType().Name
如果您使用的是 C#-6,则可以使用 nameof
运算符:
var typeName = nameof(Person);
或者正如其他人所说,GetType().Name
用于继承自 MemberInfo
的 class 名称。
无论哪种方式,您还可以使用 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}";
}
您可以使用 Type.Name 属性 - 请参阅 https://msdn.microsoft.com/en-us/library/system.reflection.memberinfo.name(v=vs.110).aspx
有几种方法可以使用 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),它会在编译时将自身替换为包含标准类型名称的常量(以下是下一个示例的反编译):
例如我有这个代码:
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
Program
ConsoleTests.Program
ConsoleTests.Program, ConsoleTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Program
尝试覆盖 ToString() 并使用 GetType() 来 return 正在使用的对象的类型。它正在 returning 信息,但它包括 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"));
}
}
}
您正在寻找 GetType().Name
如果您使用的是 C#-6,则可以使用 nameof
运算符:
var typeName = nameof(Person);
或者正如其他人所说,GetType().Name
用于继承自 MemberInfo
的 class 名称。
无论哪种方式,您还可以使用 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}";
}
您可以使用 Type.Name 属性 - 请参阅 https://msdn.microsoft.com/en-us/library/system.reflection.memberinfo.name(v=vs.110).aspx
有几种方法可以使用 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),它会在编译时将自身替换为包含标准类型名称的常量(以下是下一个示例的反编译):
例如我有这个代码:
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
Program
ConsoleTests.Program
ConsoleTests.Program, ConsoleTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Program