-1

So what i am trying to do is ;

1) I am getting a string input from user.

2) I am searching the system if project contains a function with the same name of user input.

3) If i find a function with the same name of input i am trying to execute / invoke it.

4) Usually this function is placed into another class , so i tried to create instance of class using Activators but invoke function still fails.

5) Invoke function gives me error ;

Can not invoke method : (methodName) method could not be called !

Here is the code that i am currently working on ;

 public void Execute()
 {
     // If we are only looking for function inputs.
     if (!m_canReadCls)
     {
         // If there is already a class linked into Developer Console.
         if (s_linkedType != null)
         {
             MethodInfo[] tmp = ReflectionExtensions.GetFunctions(s_linkedType);

             // Using linear search algorithm for executing functions.
             // Need to optimize it !
             if (tmp!= null)
             {
                 string funcName = m_uProps.m_inptField.text;
                 int i;
                 for (i = 0 ;i < tmp.Length;i++)
                 {
                     if ( tmp[i].Name == funcName)
                     {
                         var instance = Activator.CreateInstance( s_linkedType);
                         MethodInfo m = instance.GetType().GetMethod( funcName);
                         Invoke(m.Name, 0.0f);

                         Reset();
                     }
                 }
             }
         }
     }
 }

Any help is great , thanks :-)

I.Ozsaygi
  • 13
  • 3
  • See [Microsoft help here](https://msdn.microsoft.com/en-us/library/a89hcwhh(v=vs.110).aspx) You can call `m.Invoke`. or see [this post](https://stackoverflow.com/a/24548654/3394380) – yW0K5o Aug 02 '18 at 13:07
  • @yW0K5o Thank you so much it fixed my problem. I am newbie how can i select your answer as best one. – I.Ozsaygi Aug 02 '18 at 13:11
  • @i-ozsaygi Click accept the answer. Thank you! I am glad it solves your problem ! – yW0K5o Aug 02 '18 at 13:25

1 Answers1

0

See Microsoft help here You can call m.Invoke. or see this post

In more details

public object Invoke(
     object obj,
     object[] parameters
)

and

Type magicType = Type.GetType("MagicClass");
ConstructorInfo magicConstructor = magicType.GetConstructor(Type.EmptyTypes);
object magicClassObject = magicConstructor.Invoke(new object[]{});

// Get the ItsMagic method and invoke with a parameter value of 100

MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
object magicValue = magicMethod.Invoke(magicClassObject, new object[]{100});
yW0K5o
  • 913
  • 1
  • 17
  • 32