简体   繁体   English

Intellisense无法从扩展方法推断类型

[英]Intellisense cannot infer type from extension method

The example below compiles successfully. 下面的示例成功编译。
The compiler can infer the type of e (Customer) 编译器可以推断e的类型(客户)
My question is why Intellisense cannot do the same? 我的问题是为什么Intellisense无法做到相同?
When I type customer.SetValue( it correctly shows that the method expects 当我键入customer.SetValue(时,它正确显示该方法期望

 Expression<Func<Customer, TProperty>>

but when I type e => e. 但是当我输入e => e时。 it cannot understand that e is a Customer 它无法理解e是客户

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 我正在使用VS 2015,.NET 4.6.1

Update 更新
It is not related to Expression<> ,the method can change to 与Expression <>不相关,方法可以更改为

public static void SetValue<TEntity, TProperty>(this TEntity instance, Func<TEntity, TProperty> expression, TProperty newValue)

Update 2 更新2
I can reproduce it with 我可以用

  • VS 2015 Enterprise VS 2015企业版
  • VS 2015 Community Edition VS 2015社区版

It seems to be working in (Haven't tested other versions) 似乎可以使用(尚未测试其他版本)

  • VS 2013 Ultimate VS 2013旗舰版
  • VS 2013 Premium VS 2013高级版

我已经在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. 尽管Intellisense可以为您提供尽可能多的帮助,但它从未声称可以正确解析代码中的每个声明。 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. 传统上,这更多地是C ++及其复杂的基于宏的声明的问题,但有时您也会遇到C#的问题。

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). 您可以根据需要为此打开一个Connect问题,但是除此之外,您可以轻松地接受它,因此不要期望响应速度太快(请考虑2017年而不是2015年的Update 2)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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