vendredi 16 novembre 2018

MethodInfo.Invoke returns object does not match target type

Ive seen other questions similar to this but i cant seem to find a solution that works for my situation.

Im trying to add a class to handle method calls and return a default object if the method throws an exception.

Here is what ive got:

public class ServerConnection<T>
{

    public T2 ExecuteMethod<T2>(string methodName, T2 defaultReturnValue, params object[] p)
    {
        var result = defaultReturnValue;
        try
        {
            var baseClass = typeof(T);

            var theMethod = baseClass.GetMethod(methodName);

            result = (T2)theMethod?.Invoke(baseClass, p);

        }
        catch (Exception ex)
        {
            //shows Error "object does not match target type"
            MessageBox.Show(ex.Message);
        }

        return result;
    }

}

I'm not sure what I'm doing wrong here.





Aucun commentaire:

Enregistrer un commentaire