简体   繁体   中英

Intellisense cannot infer type from extension method

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

我已经在connect.microsoft.com上提交了错误报告

Is this expected or is it a bug?

It is neither really.

While Intellisense is there to help you as much as possible, it has never claimed to properly parse every single declaration in your code. This has traditionally been more of an issue with C++ and its complicated macro-based declarations, but every now and then you'll run into issues in C# too.

You can open a Connect issue over this if you want, but other than that it's something you can easily live with so don't expect too fast a response (think 2017 rather than 2015 update 2).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM