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>>
but when I type e => e. it cannot understand that e is a Customer
Is this expected or is it a bug?
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; }
}
}
I'm using VS 2015 ,.NET 4.6.1
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)
Update 2
I can reproduce it with
- VS 2015 Enterprise
- VS 2015 Community Edition
It seems to be working in (Haven't tested other versions)
- VS 2013 Ultimate
- VS 2013 Premium