的。如果我有以下幾點:方法返回動態類型列表
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