// Use this for initialization void Start() { /*RuntimeScript = ScriptObject.GetComponent(CompileTimeScript.GetClass()); //The non-generic form of GetComponent, mothafuckaz! * if (RuntimeScript == null) * { * //Good to put in a hard assert here * Debug.LogError(name + ": Scriptobject " + ScriptObject.name + " doesn't contain script type " + CompileTimeScript.name + "!"); * }*/ //Cache MethodInfos for the register methods, so we can make them generic later without repeating these particular reflection calls. _displayRegisterMethod = typeof(OldConsole).GetMethod("RegisterDisplayHelper", BindingFlags.Instance | BindingFlags.NonPublic); _controlRegisterMethod = typeof(OldConsole).GetMethod("RegisterControlHelper", BindingFlags.Instance | BindingFlags.NonPublic); _displayDeregisterMethod = typeof(OldConsole).GetMethod("DeregisterDisplayHelper", BindingFlags.Instance | BindingFlags.NonPublic); _controlDeregisterMethod = typeof(OldConsole).GetMethod("DeregisterControlHelper", BindingFlags.Instance | BindingFlags.NonPublic); #region Hard-Coded Test if (false) //Change to turn this test on/off. { //Hard-code adding a float function to make sure this works conceptually. MethodInfo[] methods = CompileTimeScript.GetClass().GetMethods(BindingFlags.Instance | BindingFlags.Public); //Should be the last time we call CompileTimeScript at runtime. //TEST: Find the base type of TestDisplay, cached for faster iteration when we loop through methods. Type disptype = (Type)TestDisplay.GetType().GetMethod("get_DisplayType").Invoke(TestDisplay, null); Type conttype = (Type)TestControl.GetType().GetMethod("get_ControlType").Invoke(TestControl, null); //Skip any that aren't from the user-created component int validmethods = 0; for (int i = 0; i < methods.Length; i++) { if (methods[i].DeclaringType == typeof(MonoBehaviour) || methods[i].DeclaringType.IsAssignableFrom(typeof(MonoBehaviour))) { continue; } validmethods++; //Now for the actual hard-coded testing. //Display: if (methods[i].ReturnType == disptype) { RegisterIntoDisplay(methods[i], TestDisplay, disptype); } //Control: ParameterInfo[] paramarray = methods[i].GetParameters(); if (paramarray.Length == 1 && paramarray[0].ParameterType == conttype) { print("Found control match: " + conttype + " in " + methods[i].Name); RegisterIntoControl(methods[i], TestControl, conttype); } } } #endregion //Actually set up the bindings SetUpBindings(); }
/// <summary> /// Registers a method into a display by making RegisterDisplayHandler generic and passing the given type into it. /// Calling this allows you to assign displays without handling generics yourself. /// This overload uses reflection to get the display's type, so it's better to pass in the type if it's already known. /// </summary> /// <param name="method"></param> /// <param name="display"></param> private void RegisterIntoDisplay(MethodInfo method, BaseDisplay display) { Type disptype = (Type)display.GetType().GetMethod("get_DisplayType").Invoke(display, null); RegisterIntoDisplay(method, display, disptype); }