如何获取实现接口的类名?
interface IHuman
{
public int Weight { get => 0; }
public void ShowInfo()
{
Console.WriteLine($"I am a {here must be name of class that implements this interface} and my weight is {Weight} kg.");
}
}
答案 0 :(得分:4)
这可以使用 GetType()
轻松实现:
public void ShowInfo()
{
Type t = GetType();
Console.WriteLine($"I am a {t.Name} and my weight is {Weight} kg.");
}
在这种情况下:
public class HumanA : IHuman
{
int IHuman.Weight => 5;
}
ShowInfo()
的输出将是:
I am a HumanA and my weight is 5 kg.