# Method Reference by Name

**URL:** https://discussions.unity.com/t/method-reference-by-name/601559
**Category:** Editor Extensions
**Created:** [October 15, 2015, 7:11am UTC](https://discussions.unity.com/t/method-reference-by-name/601559 "2015-10-15T07:11:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![AlexGK](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alexgk/32/531272_2.png) [@AlexGK](https://discussions.unity.com/u/AlexGK)
#### Post date: [October 15, 2015, 7:11am UTC](https://discussions.unity.com/t/method-reference-by-name/601559/1 "2015-10-15T07:11:35Z")

</div>

Hello:

I’ve been working on a couple custom inspectors for a small event handling system. I want to get references to certain kinds of methods, and be able to select them via editor. So far so good, thanks to XML acting as a database (to get standard event names) and System.Reflection (GetMethod and checking parameters).

Problem is: I’ve found no way to call those methods. I’ve got reference to the GameObject that holds the method, and both the script name and method name (as **strings** ). **Invoke won’t work for me,** because I need to store those methods for **later use** (acting as **delegates/callbacks** ), and Invoke only seems to call methods right at the moment.

Any help/ideas?

---

<div class="post-metadata">

### Author: ![liortal](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/liortal/32/238558_2.png) [@liortal](https://discussions.unity.com/u/liortal)
#### Post date: [October 15, 2015, 9:19pm UTC](https://discussions.unity.com/t/method-reference-by-name/601559/2 "2015-10-15T21:19:44Z")

</div>

Did you try to use System.Reflection for actually invoking the methods?

Once you get a MethodInfo object, you can, you can call Invoke on it to execute it.

---

<div class="post-metadata">

### Author: ![AlexGK](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/alexgk/32/531272_2.png) [@AlexGK](https://discussions.unity.com/u/AlexGK)
#### Post date: [October 15, 2015, 10:27pm UTC](https://discussions.unity.com/t/method-reference-by-name/601559/3 "2015-10-15T22:27:31Z")

</div>

Thanks for the reply. I finally found what I was looking for (thanks to a script from the NGUI guys). So, anyone in the future looking for something like this, here it is:

```csharp
//All checks done previously before call
//You want to make sure to send the proper Type, and check the method's name isn't empty and the method itself has a similar structure to MyDelegate
//Limitation: your GameObject can only have one Component of the Type you're sending (maybe storing the Component index somewhere might do the trick)

private static MyDelegate GetEventHandler(GameObject _actionGameObject, string _componentType, string _methodName){
    var actionComponent = actionGameObject.GetComponent(_componentType);
    MyDelegate d = System.Delegate.CreateDelegate(typeof(MyDelegate), actionComponent, methodName) as MyDelegate;
    return d;
}

```

Invoke() wasn’t an option because I needed the reference to the delegate for later use.
