jeudi 23 septembre 2021

How to call a function from a string [duplicate]

I'm trying to get a result from a simple function with reflection but I can't seem to get it working.

I have tried the following options:

          var classType = Type.GetType("ConfigHelper");

                //1.
                Type thisType = classType.GetType();
                MethodInfo theMethod = thisType.GetMethod("GetBool");
                var aa = theMethod.Invoke(classType, null);

                //2.
                MethodInfo method = classType.GetMethod("GetBool");
                ParameterInfo[] parameters = method?.GetParameters();
                object instance = Activator.CreateInstance(classType, null);
                var result = method.Invoke(instance, parameters);

                //3.
                var aaa = classType.GetMember("GetBool") != null;

Option 3 recognizes the function exists, but I cant seem to call the function. At option 1 theMethod will be null and with option 2 var method is null.

Function I'm trying to call

    public static bool GetBool
    {
        get
        {
            // More logic comes here later
            return true;
        }
    }

Could anyone point me in the right direction?





Aucun commentaire:

Enregistrer un commentaire