将方法调用序列化为JSON,然后反序列化并调用该方法

时间:2015-03-13 15:57:18

标签: c# serialization json.net system.reflection

我需要找到一种方法来序列化方法调用及其相关参数。这是因为我们将有效负载传递到队列(如MSMQ),然后进程将获取消息并需要反序列化有效负载并调用所需的方法。

队列的两端使用相同的C#库,但一个在Web上下文中,另一个在批处理/控制台应用程序中。

我已经在下面演示了我想做的事情,然而,我意识到这可能是不可能的。我知道我总是可以封装什么目标方法调用的元数据,并且有一个巨大的switch语句将参数/有效负载映射到一个方法,但是,如果我可以调用我想要的任何方法,它会非常酷和简洁(它是否静止并不重要。

namespace SerializableMethodCalls
{
public class DTO
{
    public string MyData { get; set; }
    public int AnInteger { get; set; }
}

public class DTO2
{
    public string MyData2 { get; set; }
    public int AnInteger2 { get; set; }
}

class Program
{
    private static Queue<string> _queue = new Queue<string>();

    static void Main(string[] args)
    {
        DTO payload = new DTO
        {
            AnInteger = 45678,
            MyData = "Test"
        };

        DTO2 payload2 = new DTO2
        {
            AnInteger2 = 534653,
            MyData2 = "test2"
        };

        DoSomething(payload);
        DoSomething(payload2);



        _queue.Enqueue(Serialize(DoSomething, payload));

        while (_queue.Count > 0)
        {
            var message = _queue.Dequeue();
            DeserializeAndCallMethod(message);
        }

    }

    private static void DeserializeAndCallMethod(string message)
    {
        // somehow deserialize the method invocation and then call the method with the serialized payload!
    }

    public static void DoSomething(DTO2 payload2)
    {
        Console.WriteLine("Done2! {0}, {1}", payload2.AnInteger2, payload2.MyData2);
    }

    public static void DoSomething(DTO dto)
    {
        Console.WriteLine("Done! {0}, {1}", dto.AnInteger, dto.MyData);
    }

    public static string Serialize(Method method, object parameter)
    {
        // somehow serialize a method call!?
    }
}

}

我有兴趣听听你的想法

非常感谢

克里斯

1 个答案:

答案 0 :(得分:2)

使用DTO和反射对象之间的演示From/To...方法进行转换:

class TypeDTO {
    public string AssemblyName;
    public string ClassName;

    public static TypeDTO FromType(Type type) {
        return new TypeDTO() {
            AssemblyName = type.Assembly.FullName,
            ClassName = type.FullName
        };
    }

    public Type ToType() {
        return ToType(AppDomain.CurrentDomain);
    }

    public Type ToType(AppDomain domain) {
        Assembly assembly = domain.GetAssemblies().Where(t => t.FullName == AssemblyName).Single();
        return assembly.GetType(ClassName);
    }
}

class MethodSignatureDTO {
    public TypeDTO DeclaringType;
    public string MethodName;
    public TypeDTO[] ParameterTypes;

    public static MethodSignatureDTO FromMethod(MethodInfo method) {
        return new MethodSignatureDTO() {
            DeclaringType = TypeDTO.FromType(method.DeclaringType),
            MethodName = method.Name,
            ParameterTypes = method.GetParameters().Select(t => TypeDTO.FromType(t.ParameterType)).ToArray()
        };
    }
    public MethodInfo ToMethod() {
        return ToMethod(AppDomain.CurrentDomain);
    }

    public MethodInfo ToMethod(AppDomain domain) {
        Type[] parameterTypes = ParameterTypes.Select(t => t.ToType(domain)).ToArray();
        return DeclaringType.ToType(domain).GetMethod(MethodName, parameterTypes);
    }
}

class MethodCallDTO {
    public MethodSignatureDTO Method;
    public object Instance;
    public object[] Arguments;

    public object Invoke() {
        return Invoke(AppDomain.CurrentDomain);
    }

    public object Invoke(AppDomain domain) {
        return Method.ToMethod(domain).Invoke(Instance, Arguments);
    }
}