I'm invoking a method dynamically in C# by using the Type.InvokeMember method call.
I have 2 methods, one that accepts a string argument, and one that accepts an int argument.
The code works fine when initialized in code, but fails after Newtonsoft.Json serialization.
Debugging doesn't help since the types seem to be correct. Here is the complete code:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace DynamicMethodCall
{
class Program
{
static void Main(string[] args)
{
List<Command> commands = new List<Command>()
{
new Command() { ClassName="Module1", MethodName="Method1", Parameters= new object[] { "1" } },
new Command() { ClassName="Module1", MethodName="Method2", Parameters= new object[] { 1 } }
};
foreach (var command in commands)
{
string result = command.Invoke();
Console.WriteLine(result);
}
File.WriteAllText("commands.json", JsonConvert.SerializeObject(commands));
commands = JsonConvert.DeserializeObject<List<Command>>(File.ReadAllText("commands.json"));
foreach (var command in commands)
{
string result = command.Invoke();
Console.WriteLine(result);
}
Console.ReadLine();
}
}
public class Command
{
public string ClassName { get; set; }
public string MethodName { get; set; }
public object[] Parameters { get; set; }
public string Invoke()
{
try
{
Type type = Type.GetType("DynamicMethodCall." + ClassName);
object instance = Activator.CreateInstance(type);
string response = (string)type.InvokeMember(MethodName,
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
null, instance, Parameters);
return response;
}
catch (Exception ex)
{
return ex.Message;
}
}
}
public class Module1
{
public string Method1(string x)
{
return $"You called Method1 in Module1 with arg {x}.";
}
public string Method2(int x)
{
return $"You called Method2 in Module1 with arg {x}.";
}
}
}
It returns
- You called Method1 in Module1 with arg 1.
- You called Method2 in Module1 with arg 1.
- You called Method1 in Module1 with arg 1.
- Method 'DynamicMethodCall.Module1.Method2' not found.
Meaning that after Newtonsoft serialize-deserialize, the method with int argument doesn't work. Anybody know what seems to be the problem?
Aucun commentaire:
Enregistrer un commentaire