代码之家  ›  专栏  ›  技术社区  ›  James Alexander

expression.call和“发现不明确的匹配”

  •  8
  • James Alexander  · 技术社区  · 15 年前

    我正在尝试编写一个表达式,该表达式将对属性调用ToString,并将其值赋给局部变量。但是,在带有ToString重载的对象实例上调用ToString会导致抛出“ambigous match found”的异常。下面是一个例子:

    var result = Expression.Variable(typeof(string), "result");
    var matchTypeParameter = Expression.Parameter(typeof(MatchType), "matchType");
    var targetProperty = Expression.Property(leadParameter, target);
    
    var exp = Expression.Block(
      //Add the local current value variable
      new[] { result },
    
      //Get the target value
      Expression.Assign(result, Expression.Call(targetProperty, typeof(string).GetMethod("ToString"), null))
    
    );
    

    如果实例有重载,如何调用ToString?谢谢!

    1 回复  |  直到 15 年前
        1
  •  13
  •   Kirk Woll    15 年前

    替换:

    typeof(string).GetMethod("ToString")
    

    用:

    typeof(string).GetMethod("ToString", Type.EmptyTypes)
    

    换句话说,获取名为“ToString”的方法,该方法接受零个参数(空类型数组)。

    推荐文章