我有简化的程序,它使用表达式:
public class Program
{
public static IEnumerable<char> foo1()
{
// For some reason I cannot change result type
return new char[] { 's', '1' };
}
public static string foo2()
{
// For some reason I cannot change result type
return "s1";
}
static void Main()
{
Expression cond = Expression.Condition(Expression.Constant(true),
Expression.Call(typeof(Program).GetMethod("foo1")),
Expression.Call(typeof(Program).GetMethod("foo2")));
}
}
在执行过程中,我有以下错误
参数类型不匹配。
据我了解,c#不使用隐式类型转换。我怎么解决这个问题?
构建表达式时,不会为您添加隐式转换。您需要使用明确指出要应用转换操作符Expression.Convert
。
Expression cond = Expression.Condition(Expression.Constant(true),
Expression.Call(typeof(Program).GetMethod("foo1")),
Expression.Convert(Expression.Call(typeof(Program).GetMethod("foo2")), typeof(IEnumerable<char>)));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句