jeudi 13 août 2020

Object of type 'MyClass' cannot be converted to type 'System.Object[]' C#

I've been looking into this for a couple of hours but so far haven't gotten any luck. Here's my C# code:

myClassInstance = new MyClass("MyParam", 1);
object[] args = new object[1] { myClassInstance };

MethodInfo methodInfo = GetType().GetMethod(myMethod, BindingFlags.NonPublic | BindingFlags.Instance);
string method = (string)methodInfo.Invoke(this, args);

I have MethodInfo and System.Reflection imported. The Unity error is this:

ArgumentException: Object of type 'SystemController' cannot be converted to type 'System.Object[]'

It doesn't point to a specific line in the code, but from what I can tell it seems to be an issue with converting the myClassInstance variable to an object, which doesn't make sense to me, as I believed everything in C# inherited from System.Object.

Here is MyClass:

public class MyClass 
{
    public string var1;
    public int var2;

    public MyClass(string param1, int param2) 
    {
        var1 = param1;
        var2 = param2;
    }
}

Clearly, I'm not showing the entire class, but the only difference is that there are more variables and parameters to store. Those shouldn't change anything, so I won't bore you with them. It's just a class with a constructor, not inheriting from anything. Any help I could get with this would be greatly appreciated. Let me know if you need more info.





Aucun commentaire:

Enregistrer un commentaire