如果声明是接口,则编译器无法识别通用属性 [英] Compiler doesnt recognise property in generic if declaration is an interface

查看:87
本文介绍了如果声明是接口,则编译器无法识别通用属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看下面的内容演示我的Visual Studio 2017编译器问题

public interface IFoo
{
    string Key { get; set; }
}

public class Foo : IFoo
{
    public string Key { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        PrintFoo(new Foo() { Key = "Hello World" });
        Console.ReadLine();
    }

    private static void PrintFoo<T>(T foo) where T : IFoo
    {
        //set breakpoint here and try to look at foo.Key
        Console.WriteLine(foo.Key);
    }
}

当我在PrintFoo方法内创建断点并想查看fooKey属性时,Visual Studio不会为我提供工具提示. 通过将foo.Key添加到监视窗口,我收到以下错误:

错误CS1061:"T"不包含键"的定义,并且没有 扩展方法'Key'可以接受类型'T'的第一个参数 找到(您是否缺少using指令或程序集引用?)

当我将通用声明更改为Foo而不是IFoo时,编译器可以访问'Key'属性,因此:

private static void PrintFoo<T>(T foo) where T : Foo
{
    //set breakpoint here and try to look at foo.Key
    Console.WriteLine(foo.Key);
}

有办法使它工作吗?

这两种方法都是在本地窗口中查看,然后将鼠标悬停在foo上以获取工具提示,而不是扩展属性.

在监视窗口中添加foo.Key或将?foo.Key写入立即窗口会带来上述错误,并且当您将鼠标悬停在foo.KeyKey上时,不会得到工具提示.

在Visual Studio 2015、2017中进行了测试.

解决方案

错误已在Visual Studio 2019中修复: https://developercommunity.visualstudio.com/content/problem/216341/compiler-doesnt-recognise-property-in-generic-if-d.html

Ivan Basov [MSFT]发表评论:

在当前的Visual中似乎无法重现该问题 工作室.我在VS 2019 Preview 2中进行了尝试.该方案工作正常. 谢谢您的反馈!

还尝试在我的预览版1.1上重现它,并且它也在那里修复.

还尝试提供最新版本的Visual Studio 2017(15.9.5),并可以报告该版本已修复.

Have a look at the following that demonstrates my issue with Visual Studio 2017 compiler

public interface IFoo
{
    string Key { get; set; }
}

public class Foo : IFoo
{
    public string Key { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        PrintFoo(new Foo() { Key = "Hello World" });
        Console.ReadLine();
    }

    private static void PrintFoo<T>(T foo) where T : IFoo
    {
        //set breakpoint here and try to look at foo.Key
        Console.WriteLine(foo.Key);
    }
}

When I make a breakpoint inside the PrintFoo method and want to look at the Key property of foo Visual Studio wont provide a tooltip for me. By adding the foo.Key to the watch window I receive the following error:

error CS1061: 'T' does not contain a definition for 'Key' and no extension method 'Key' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)

When I change the generic declaration to Foo instead of IFoo the compiler can acces the 'Key' property, so this:

private static void PrintFoo<T>(T foo) where T : Foo
{
    //set breakpoint here and try to look at foo.Key
    Console.WriteLine(foo.Key);
}

Is there a way to make it work?

Edit:

Both, looking at the local window and mouse over foo to get the tooltip and than expanding the properties works.

Adding foo.Key to the watch window or writing ?foo.Key into immediate window brings the mentioned error, and you wont get a tooltip when you mouse over Key of foo.Key

Tested with Visual Studio 2015, 2017.

解决方案

Bug got fixed in Visual Studio 2019: https://developercommunity.visualstudio.com/content/problem/216341/compiler-doesnt-recognise-property-in-generic-if-d.html

Comment by Ivan Basov [MSFT]:

It seems that the issue is not reproducible in the current Visual Studio. I tried it with VS 2019 Preview 2. The scenario works fine. Thank you for your feedback!

Also tried to reproduce it on my Preview Version 1.1 and it is fixed there aswell.

Gave the latest Version of Visual Studio 2017 (15.9.5) a try aswell and can report it got fixed there aswell.

这篇关于如果声明是接口,则编译器无法识别通用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆