public static TMostDerived Default <TMostDerived>() { var type = typeof(TMostDerived); TMostDerived value = default(TMostDerived); if (msDefaultTypes.TryGetValue(type, out var func)) { value = (TMostDerived)func(); } else if (type.IsClass) { value = (TMostDerived)Activator.CreateInstance(type); } if (value == null) { throw new Exception(string.Format("The value is null, The Type {0} must support the Default constructor.", type.Name)); } return(value); }
/// <summary> /// The high performance API for GetComponent /// </summary> /// <typeparam name="TMostDerived">The Most Derived Type</typeparam> /// <returns></returns> public TMostDerived GetComponentCache <TMostDerived>() where TMostDerived : Component { DebugUtility.Assert(this != null, "this MonoBehaviour has destroyed."); var type = typeof(TMostDerived); mComponentCaches.TryGetValue(type, out var component); if (component == null) { TMostDerived com = GetComponent <TMostDerived>(); if (com == null) { mComponentCaches.Remove(type); } else { mComponentCaches[type] = com; } return(com); } return(component as TMostDerived); }