7

最終的な式のために 2 つの式を連結したい

Expression<Func<T, string>>

だから私は式の下のコードを作成した

タイプ 'System.Int32' の式は、メソッド 'System.String Concat(System.String, System.String)' のタイプ 'System.String' のパラメータには使用できません

式を次のように変換すると

var conversion = Expression.Convert(memberExpression, typeof (string));

型 'System.Int32' と 'System.String' の間に強制演算子が定義されていません。

解決するのを手伝ってください

コード

MethodInfo bodyContactMethod = typeof (string).GetMethod("Concat",new[] {typeof (string), typeof (string)});

ParameterExpression parameter = Expression.Parameter(typeof (T));

body = Expression.Call(bodyContactMethod, cons, memberExpression);

return Expression.Lambda<Func<T, string>>(body, parameter);
4

4 に答える 4

10

文字列にキャストしようとする代わりに、オブジェクトにキャストしてから ToString() を呼び出すことができます。

var converted = member.ToString();

式としては、次のようになります。

var convertedExpression = Expression.Call(
                     Expression.Convert(memberExpression, typeof(object)),
                     typeof(object).GetMethod("ToString"));
于 2013-07-25T15:54:34.353 に答える
0

少し遅れていますが、Richard Deemingの答えを拡張します。

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

署名をそのままにしておくことを許可しながら、それは問題なく機能するはずです。

于 2014-11-16T09:35:51.480 に答える