Join  |  Sign in
Getting methods from Script

07-Sep-2017 05:10 AM

Aresto

Aresto

Posts: 3

is there is any way to get List of methods names of a script I want to run . 

I simply want to have combo box with all methods available so that the user can choose which method to run.


  Reply

07-Sep-2017 07:43 PM

Andrew Medvedev

Andrew Medvedev

Posts: 50

Hi,

Once script is compiled you can query types and methods in Script assembly using reflection, below is the code:

Please note that this code returns only static methods, if you need to call public methods, you will need to change BindingFlags.Static to BindingFlags.Instance and instantiate class defined in the script code using System.Activator.CreateInstance()

                if (!scriptRun.Compiled)
                {
                    if (!scriptRun.Compile())
                    {
                        return;
                    }
                }
                var assembly = scriptRun.ScriptHost.ScriptAssembly;
                IList<string> methods = new List<string>();
                foreach (Type type in assembly.GetTypes())
                {
                    if (type != null)
                    {
                        foreach (var method in type.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public))
                            methods.Add(method.Name);
                    }
                }
regards,
Andrew
  Reply