Kee*_*ker 8 .net generics methods c#-2.0
我一直在玩仿制药,我一直在看一些奇怪的东西.我希望你们有解释!为了使一切更容易,我把"问题"放在一个例子中:
namespace Lab
{
public class Animal
{
public Animal(string sound)
{
this.Sound = sound;
}
public string Sound { get; private set; }
public void Kick()
{
Printer.Print(this, Sound);
}
}
public class Dog : Animal
{
public Dog() : base("Bark, bark! I'll bite you!") { }
}
public class Printer
{
public static void Print<T>(T obj, string message)
{
System.Console.WriteLine("{0} says '{1}' \n", typeof(T).FullName.PadRight(10), message);
}
}
public static class Program
{
static void Main(string[] args)
{
Animal bird = new Animal("Tweet!");
Dog dog = new Dog();
System.Console.WriteLine("Kick bird:");
bird.Kick();
System.Console.WriteLine("Kick dog:");
dog.Kick();
System.Console.WriteLine("Print kick dog:");
Printer.Print(dog, dog.Sound);
System.Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我的实验室里有两只动物:一只狗和一只鸟.当我"踢"那些动物时,它们会发出声音.打印机将打印声音和动物类型.当我运行该程序时,它打印:
踢鸟:Lab.Animal说'推特!'
踢狗:Lab.Animal说'树皮,树皮!我会咬你的!"
打印踢狗:Lab.Dog说'树皮,树皮!我会咬你的!"
为什么狗的第一脚告诉我它的类型Lab.Animal?
而且......我怎么能让它回来Lab.Dog?
狗的第一步告诉你类型参数的编译时类型是Lab.Animal.换句话说,您的Animal.Kick方法是有效的:
Printer.Print<Animal>(this, Sound);
Run Code Online (Sandbox Code Playgroud)
类型参数不是多态确定的 - 它们是在编译时确定的.当一个调用的类型参数实际上是调用上下文的类型参数时,它变得更加复杂,但它基本上是同一类事物.
要说它Lab.Dog,你必须获得对象的实际执行时类型,例如使用
obj.GetType().FullName
Run Code Online (Sandbox Code Playgroud)