在具有各种 Func 委托重载的函数中使用匿名方法时,我在 Visual Studio 2010 中遇到了一些奇怪的行为。
我在下面创建了一个小型复制类。
考虑这个 ListViewAdapter 类
namespace LambdaTestApp
{
public class ListViewAdapter<T>
{
private Func<int, string, int, string> _converter1;
private Func<RefType1, string, string> _converter2;
public ListViewAdapter(int arg1, Func<int, string, int, string> converter)
{
_converter1 = converter;
}
public ListViewAdapter(int arg1, Func<RefType1, string, string> converter)
{
_converter2 = converter;
}
public static ListViewAdapter<T> MockPopulate(int arg, Func<int, string, int, string> converter) {
ListViewAdapter<T> instance = new ListViewAdapter<T>(arg, converter);
return instance;
}
public static ListViewAdapter<T> MockPopulate(int arg, Func<RefType1, string, string> converter)
{
ListViewAdapter<T> instance = new ListViewAdapter<T>(arg, converter);
return instance;
}
}
public class RefType1
{
public string Property1 { get; set; }
}
}
以下代码使用带有 lambda 的重载:
namespace LambdaTestApp
{
class Program
{
static void Main(string[] args)
{
ListViewAdapter<RefType1>.MockPopulate(1, (item, str) =>
{
var myItem = item;
return str;
});
}
}
}
它应该解析为Func<RefType1, string, string>
并且第一个参数应该是RefType1
,但问题是它不是item
Visual RefType1
Studio 而是将其视为int
.
问题:Func 委托之间是否存在不明显的有效转换,或者这是 Visual Studio IntelliSense 错误?