【发布时间】:2019-07-09 21:09:50
【问题描述】:
下面的例子编译成功。
编译器可以推断 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
我可以复制它
- VS 2015 企业版
- VS 2015 社区版
它似乎在工作(尚未测试其他版本)
- VS 2013 终极版
- VS 2013 高级版
【问题讨论】:
-
我已经发生过几次这种情况,大约每隔一周一次。关闭和重新打开 VS 通常会为我修复它,但它仍然很烦人。
-
我确定已经省略了一些代码以保持问题较小,但我可以问一下扩展的目的来设置属性吗?
-
看起来像一个错误,可能与 Roslyn 有关。
-
@Wobbles,不,这是完整的例子
-
@JRLambert 我已经重新启动了我的 VS/PC,但我总是遇到这个问题。我会打开一个错误报告
标签: c# .net visual-studio intellisense