- Home /
invoke method with reflection C#
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
public class blblb : MonoBehaviour
{
void Start()
{
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod("bla");
theMethod.Invoke(this, null);
}
private void bla()
{
Debug.Log("enters");
}
}
NullReferenceException: Object reference not set to an instance of an object blblb.Start () (at Assets/blblb.cs:13)
What am I doing wrong to get this error?
P.S I know there's Invoke method in unity. I need to know how to use the reflection way.
"this" is a keyword and can not be used as the name of a function. You probably need to read the Scripting API to see how to use Invoke.
Invoke wants the name of a method as a String and the time before it is invoked.
Invoke("$$anonymous$$y$$anonymous$$ethod", 0);
This is basically the same as what awplays49 said.
The first parameter is the object holding the method instance.
Answer by fafase · Feb 15, 2015 at 04:19 PM
This is an example on how to get instance methods. It works for me so there is no reason it should break for you.
private Action myAction = ()=>{};
void Start()
{
System.Type ourType = this.GetType();
MethodInfo mi= ourType.GetMethod("MyMethod", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
if (mi!= null){
myAction = (Action)Delegate.CreateDelegate(typeof(Action), this, mi);
}
}
then you can call myAction(); like any other methods.
This has the advantage of allowing sub class to contain methods or not just like Unity does for Update/LateUpdate or other events. Your top class may contain all the checks and the sub class just implements or not.
So when you inherit from $$anonymous$$onoBehaviour does that class use Actions to call the Update method (etc) in your child class?
I would assume that each mb has a bunch of private actions that are called each frame from the engine. once in the constructor a similar approach to the one I show could be used, hence the slow process of instantiation vs pooling. Reflection is used for sure, how exactly I cannot say
@fafase Thank you, first for the explanation. I've got a few questions though.
This has the advantage of allowing sub class to contain methods
Didn't quite understand what did you mean by this.(most likely it's totaly understandable, it's just me sometimes, being unable to get the sense of things from the first glance)
Your top class may contain all the checks and the sub class just implements or not
Isn't this totaly possible with virtual methods and props, like protected virtual bool CheckSmth()
and the sub class can choose whether to implement it or not?
http://prntscr.com/65j57f - I saw what they say here for .Instance. But still, as far as I understood it specifies that it should search for members also in the current class. So my question is: System.Type ourType = this.GetType();
isn't here the place we specify the instance(class) where to search at for the given member?
Anyway, very grateful for your answer. I think I learnt much from it!
Indeed it is possible to perform the same action with inheritance. This is what XNA framework was doing.
It is just a matter of how you want it to be done. Reflection may be faster (to be confirmed) while polymorphism requries more typing (override keyword...big deal) and has the benefit of autocompletion (the compiler knows the method from parent class while in reflection, no way it can know about it.).
Your call.
I understand. Anyway am I doing anything wrong or it's just impossible to call a static methods with the myAction var(I added BindingFlags.Static) but still doesn't work.
Answer by ByteSheep · Feb 15, 2015 at 03:38 PM
msdn documentation on GetMethod().
"Searches for the public method with the specified name."
Simply make your bla method public ;)
Note: The easiest way to debug this kind of stuff is to use Debug.Log() statements.
void Start ()
{
Type thisType = this.GetType();
Debug.Log ("Type: " + thisType);
MethodInfo theMethod = thisType.GetMethod("bla");
Debug.Log ("MethodInfo: " + theMethod);
theMethod.Invoke(this, null);
}
theMethod variable is null, since the bla method is private and cannot be found (hence the NullReferenceException).
You can access private methods with
$$anonymous$$ethodInfo mi= type.Get$$anonymous$$ethod("$$anonymous$$ethodName", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
@merry_christmas Thanks for the time spent on my problem! +1, your answer definatelly helped me a lot, but still fafase's one is a little bit more satisfying + the comments he wrote.
Answer by awplays49 · Feb 15, 2015 at 02:23 PM
Well first of all, don't type "this", because thats just going to cause errors…
Change your code to:
Invoke ("YourFunction", 0);
assuming the name of your function is YourFunction.
Come on man, please read the whole question, especially the P.S
He is simply telling you to read the P.S of the question. That is he is not using Invoke from $$anonymous$$onoBehaviour but nvoke from .NET which takes Object as first parameter.