[英]Intellisense cannot infer type from extension method
下面的示例成功編譯。
編譯器可以推斷e的類型(客戶)
我的問題是為什么Intellisense無法做到相同?
當我鍵入customer.SetValue(時,它正確顯示該方法期望
Expression<Func<Customer, TProperty>>
但是當我輸入e => e時。 它無法理解e是客戶
這是預期的還是錯誤?
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace ConsoleApplicationExpressionTree
{
class Program
{
static void Main(string[] args)
{
Customer customer = new Customer();
customer.SetValue(e => e.Age, 10);
customer.SetValue(e => e.Name, "TheName");
//type here
}
}
public static class Extentions
{
public static void SetValue<TEntity, TProperty>(this TEntity instance, Expression<Func<TEntity, TProperty>> expression, TProperty newValue)
{
if (instance == null)
throw new ArgumentNullException();
var memberExpression = (MemberExpression)expression.Body;
var property = (PropertyInfo)memberExpression.Member;
property.SetValue(instance, newValue, null);
}
}
public class Customer
{
public string Name { get; set; }
public int Age { get; set; }
}
}
我正在使用VS 2015,.NET 4.6.1
更新
與Expression <>不相關,方法可以更改為
public static void SetValue<TEntity, TProperty>(this TEntity instance, Func<TEntity, TProperty> expression, TProperty newValue)
更新2
我可以用
似乎可以使用(尚未測試其他版本)
我已經在connect.microsoft.com上提交了錯誤報告
這是預期的還是錯誤?
兩者都不是。
盡管Intellisense可以為您提供盡可能多的幫助,但它從未聲稱可以正確解析代碼中的每個聲明。 傳統上,這更多地是C ++及其復雜的基於宏的聲明的問題,但有時您也會遇到C#的問題。
您可以根據需要為此打開一個Connect問題,但是除此之外,您可以輕松地接受它,因此不要期望響應速度太快(請考慮2017年而不是2015年的Update 2)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.