繁体   English   中英

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

[英]Intellisense cannot infer type from extension method

下面的示例成功编译。
编译器可以推断e的类型(客户)
我的问题是为什么Intellisense无法做到相同?
当我键入customer.SetValue(时,它正确显示该方法期望

 Expression<Func<Customer, TProperty>>

但是当我输入e => e时。 它无法理解e是客户

这是预期的还是错误?

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

更新
与Expression <>不相关,方法可以更改为

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

更新2
我可以用

  • VS 2015企业版
  • VS 2015社区版

似乎可以使用(尚未测试其他版本)

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

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

这是预期的还是错误?

两者都不是。

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

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

暂无
暂无

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

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