class Building {
}
class Home: Building {
}
private List<Building> buildings;
public T[] FindBuildings<T>() {
return buildings.FindAll(x => x is T).ToArray() as T[];
}
I have a list to save all the buildings on the map, and write a function to find the certain building type.
I want using FindBuildings<Home>()
to get an array that contains all home buildings on the map, but when I run the code, I found the function always return null, it seems as
did not work. Does this mean as
can not using generic type parameter?
So, I change the code:
private List<Building> buildings;
public Building[] FindBuildings<T>() {
return buildings.FindAll(x => x is T).ToArray();
}
using FindBuildings<Home>() as Home[]
to access the home buildings on the map. But...it still get null.
I saw a Unity3d's example, show me the using 'as' to convert array type, why I failed?
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void OnMouseDown() {
HingeJoint[] hinges = FindObjectsOfType(typeof(HingeJoint)) as HingeJoint[];
foreach (HingeJoint hinge in hinges) {
hinge.useSpring = false;
}
}
}