Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
class baker: unit {} class killer: unit{} void Round(unit first, unit second, byte fA, byte sA) { how to create array of "unit first" type objects? }
我将面包师或杀手作为参数传递,我想在方法中克隆这个特定的类,但我不明白如何
对于简单的方法:
if(first is baker) { // Create array of bakers }
或者您可以像这样使用泛型:
void Round<T>(T first, T second,...) { // Create the list of the type new List<T>();
或者您可以使用:
Array.CreateInstance(first.GetType(), length);
在最坏的情况下,您可以使用 first.GetType() 并使用反射来创建一个数组。但这有点复杂