的。如果我有以下几点:方法返回动态类型列表
class Super {
//stuff
}
class Sub1 : Super {
//stuff
}
class Sub2 : Super {
//stuff
}
class Sub3 : Super {
//stuff
}
运行类
class Run {
//list of type Super with a bunch of sub objects (sub1,sub2,sub3)
List<Super> superList = sub1,2,3 list;
List<Super> partialSuperList;
void doStuff() {
Type subObjectType = superList[0].GetType();
if (subObjectType == typeof(sub1)) {
partialSuperList = categorize(subObjectType);
} else if (subObjectType == typeof(sub2)) {
partialSuperList = categorize(subObjectType);
} else if (subObjectType == typeof(sub3)) {
partialSuperList = categorize(subObjectType);
}
}
List<Super> categorize(Type type) {
List<type> subTypeList = new List<type>();
//loop through subtype list and add it to a super type list
return SuperList;
}
}
,你可以看到我想要动态地创建列表,并附有“型”来了从论证到方法,我是这样做的还是有另一种方法来做到这一点?
这听起来像是一个可怕的设计... – 2014-10-09 13:19:43
@PatrickHofman这是为什么,我试图避免重蹈内4线if/else语句,只是打电话,做它 – 2014-10-09 13:20:36
并非一切都意味着一个1线法是通用的,你真的不应该为你所期望的每一种类型创建'if',这只是不好的做法。 – 2014-10-09 13:28:02