H
H
HellFingers2020-02-09 12:28:42
C++ / C#
HellFingers, 2020-02-09 12:28:42

Code reflection error?

Hello, there is a Main method, which receives a list and a string as input, it is necessary, based on the value of the string, to form the name of the method and call it, passing this list and string. I decided to use reflection for this task, but it gives an error in the line VariableNames = Finder.Invoke(this, new object[] { TextFromFile, FromLanguage });
Writes that it is impossible to convert the object type to ArrayList, I don’t understand where my error is. Thanks in advance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
namespace VariableFinder
{
    public class Finder : MonoBehaviour
    {
        public ArrayList Main(ArrayList TextFromFile, string FromLanguage)
        {
            ArrayList VariableNames;
            string MethodName = "Finder" + FromLanguage.ToUpper();
            MethodInfo Finder = this.GetType().GetMethod(MethodName);
            VariableNames  = Finder.Invoke(this, new object[] { TextFromFile, FromLanguage });
            return VariableNames;
        }



        //Методы поиска имен переменных

        public ArrayList FinderPYTHON(ArrayList TextFromFile, string FromLanguage)
        {
            ArrayList VariableNames = new ArrayList();
            VariableNames.Add("one");
            return VariableNames;
        }
        
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Yudakov, 2020-02-09
@HellFingers

The result of the method call must be cast to the variable type VariableNames:

VariableNames = (ArrayList)Finder.Invoke(this, new object[] { TextFromFile, FromLanguage });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question