mardi 26 avril 2016

How to serialize method call expression with arguments?

I have a call to a remote service which is described as following:

var user = new User { Name = "check" };
WcfService<IMyService>.Call(s => s.MyMethod(1, "param", user, new Entity { ID = 2 }));

In my Call method, I need to serialize this method call to JSON, which will be put in the WebSphere queue:

{
    "Interface": "IMyService",
    "Method": "MyMethod",
    "Arguments": [
        1,
        "param",
        {
            "Name": "check"
        },
        {
            "ID": 2
        }
    ]
}

I know how to obtain interface and method names, but I cannot obtain non-constant values:

public static class WcfService<TInterface>
{
    public static void Call(Expression<Action<TInterface>> expr)
    {
        var mce = (MethodCallExpression)expr.Body;

        string interfaceName = typeof(TInterface).Name;
        string methodName = mce.Method.Name;

        var args = mce.Arguments
            .Cast<ConstantExpression>()
            .Select(e => e.Value)
            .ToArray();
    }
}

This code works for 1 and "param", but does not work for user and new Entity { ID = 2 }) since they are FieldExpression and NewExpression respectively. How to get the actual values, passed to a function call, instead of their expression representation?





Aucun commentaire:

Enregistrer un commentaire