如果声明是接口,则编译器无法识别通用属性 [英] Compiler doesnt recognise property in generic if declaration is an interface
问题描述
看看下面的内容演示我的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
方法内创建断点并想查看foo
的Key
属性时,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.Key
的Key
上时,不会得到工具提示.
在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屋!