我有一个抽象类,我更多地将其用作接口(interface)。我的部分代码接受了抽象类,但我需要弄清楚继承它的是哪个类,以便我可以使用该类填充列表。
abstract class thingy
{
//methods, variables, etc
}
class awsomeThing : thingy
{
//Imagine there are more of these classes with different names...
}
我仅限于将抽象类(thingy)作为类型传递,但需要它的 awsomeThing
类型?我该怎么做呢?
编辑
太清楚了我在答案中寻找什么,使用 awsomeThing
的类有一个接近于此的方法:
void Start(thingy wantedType){
List<thingy> collectionOfStuff = new List<thingy>( );
for(int i = 0; i < 3; i++){
//Here I want to add the type awsomeThing or one of the other classes
//that derive from thingy but I don't know which one it is
collectionOfStuff.Add(new ????); //what is wantedType in reality?
}
}
最佳答案
关于C# 从继承的类中获取类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36271398/