我有一个接口,由更多存储了附加信息的类实现.我需要能够转换实现而不明确地说它是这一个实现.
public interface IAnimal
{
string Name { get; set; }
Type GetType();
}
public class Dog : IAnimal
{
public string Name { get; set; }
Type GetType() {return typeof(Dog);}
public int TimesBarked { get; set; }
}
public class Rhino : IAnimal
{
public string Name { get; set; }
Type GetType() {return typeof(Rhino);}
public bool HasHorn { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
通过大多数代码我使用界面没有问题,但在某些时候我需要获得原始类型的实现.
IAnimal animal = new Dog
{
Name = "Ben",
TimesBarked = 30
}
// Doing …Run Code Online (Sandbox Code Playgroud) c# ×1