Intellisense无法从扩展方法推断类型 [英] Intellisense cannot infer type from extension method
本文介绍了Intellisense无法从扩展方法推断类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的示例成功编译.
编译器可以推断e(客户)的类型
我的问题是为什么Intellisense无法做同样的事情?
当我键入 customer.SetValue(时,它正确显示该方法期望
The example below compiles successfully.
The compiler can infer the type of e (Customer)
My question is why Intellisense cannot do the same?
When I type customer.SetValue( it correctly shows that the method expects
Expression<Func<Customer, TProperty>>
但是当我键入 e => e.时,它无法理解e是客户
but when I type e => e. it cannot understand that e is a Customer
这是预期的还是错误?
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
I'm using VS 2015 ,.NET 4.6.1
更新
它与Expression<>不相关,方法可以更改为
Update
It is not related to Expression<> ,the method can change to
public static void SetValue<TEntity, TProperty>(this TEntity instance, Func<TEntity, TProperty> expression, TProperty newValue)
更新2
我可以用
Update 2
I can reproduce it with
- VS 2015 Enterprise
- VS 2015社区版
似乎可以使用(尚未测试其他版本)
It seems to be working in (Haven't tested other versions)
- VS 2013 Ultimate
- VS 2013 Premium
推荐答案
我已在 查看全文