I'm having one method like the following, where Days
is an Enum
having values from Sunday
to Saturday
, Which I'm calling using reflection. I'm having lots of similar methods with different signatures And the input parameters are also dynamic which I'm getting as a JSON.
Public Sub ReflectionMethod(ByVal stringParam As String, ByVal enumParam As Days)
Console.WriteLine($"Executed:ReflectionMethod | stringParam : {stringParam}; enumParam : {enumParam}")
End Sub
The following code is used to parse the JSON and call the method using reflection.
Dim inputJson As String = "['Sample Input',2]"
Dim lstObjects As List(Of Object) = JsonConvert.DeserializeObject(Of List(Of Object))(inputJson)
Dim method As MethodInfo = consoleObject.GetType.GetMethod("ReflectionMethod")
method.Invoke(consoleObject, lstObjects.ToArray)
Throwing the following error while executing the above code, During debugging noticed that method.Invoke()
method is getting failed nothing wrong with the deserialization.
Exception: Object of type 'System.Int64' cannot be converted to type 'VBConsole.Days'.
Please note: This is not the actual code that I'm using, just created a console application for easy debugging, and an answer in C#
is also appreciated, That's why tagged it to c#