Intellisense cannot infer type from extension method

ぃ、小莉子 提交于 2019-12-12 20:17:13

问题


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

回答1:


I've filed a bug report at connect.microsoft.com




回答2:


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).



来源:https://stackoverflow.com/questions/34883559/intellisense-cannot-infer-type-from-extension-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!