LINQ Expression Conversion / Concat from Int to string

后端 未结 4 394
南方客
南方客 2020-12-19 07:33

I want to Concat two expressions for the final expression

Expression>

So I have created expression belwo code

相关标签:
4条回答
  • 2020-12-19 07:54

    It can be further simplified to:

    var convertedExpression = Expression.Call(
                         memberExpression,
                         typeof(object).GetMethod("ToString"));
    
    0 讨论(0)
  • 2020-12-19 07:55

    Instead of trying to cast to string, you could try casting to object then calling ToString(), as though you were doing:

    var converted = member.ToString();
    

    As an Expression, it will look something like this:

    var convertedExpression = Expression.Call(
                         Expression.Convert(memberExpression, typeof(object)),
                         typeof(object).GetMethod("ToString"));
    
    0 讨论(0)
  • 2020-12-19 08:03

    Rather than calling string.Concat(string, string), you could try calling string.Concat(object, object):

    MethodInfo bodyContactMethod = typeof (string).GetMethod("Concat", 
       new[] { typeof(object), typeof(object) });
    
    0 讨论(0)
  • 2020-12-19 08:05

    To expand on Richard Deeming's answer even though it's a little late.

    Expression.Call(
        typeof(string).GetMethod("Concat", new[] { typeof(object), typeof(object) }),
        Expression.Convert(cons, typeof(object)),
        Expression.Convert(memberExpression, typeof(object))
    );
    

    That should work just fine while allowing the signature to stay as you have it.

    0 讨论(0)
提交回复
热议问题