jeudi 2 août 2018

Calling a function from other class using Invoke

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 :-)





Aucun commentaire:

Enregistrer un commentaire