- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在使用 Visual Studio 10(现在也是 11)时遇到了一个奇怪的错误。我有一个扩展方法
public static S Foo<S, T>(this S s) where S : IEnumerable<T>
{
return s;
}
现在如果我打电话
"".Foo(); // => 'string' does not contain a definition for 'Foo' and no extension method 'Foo' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
我根本不了解幕后发生的事情。烦人的部分是 intellisense 列出了 Foo
对于 IEnumberable<T>
s。充其量它应该给出一个 type can't be inferred error
.
如果我这样调用它:
Extension.Foo(""); // => The type arguments for method 'Extension.Foo<S,T>(S)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
为什么不能在上述情况下推断出类型?
更多:
假设我有:
public static S Foo<S, T>(this S s, T t) where S : IEnumerable<T>
{
return s;
}
如果我调用:
"".Foo(1);
这里的类型推断非常聪明,可以告诉我 Foo
应该返回 IEnumerable<int>
和 string
还不止这些!!
所以如果编译器可以知道Foo
期待一个 char 作为第一个参数,那么为什么我的第一个例子不能编译? 换句话说,为什么在第一个例子中编译器知道 T
在那种情况下是 char?
正如预期的那样,这适用于第二个示例:
"".Foo('l');
我只是想知道为什么不能 T
被推断为char
在第一个例子中,毕竟字符串是 IEnumberable<char>
.
我从 SLaks 那里得到了答案。但考虑到编译器在公开可用方法以对对象进行操作时也会考虑泛型约束,C# 不执行此操作(一种类型推断)真是太奇怪了。
换句话说:
public static S Foo<S, T>(this S s)
{
return s;
}
制造 Foo
适用于所有object
public static S Foo<S, T>(this S s) where S : IEnumerable<T>
{
return s;
}
制造 Foo
适用于所有IEnumerable<T>
因为它知道 S
是IEnumerable<T>
.所以我在想 C# 甚至会推断 T
的类型!谢谢大家! ;)
最佳答案
类型推断引擎不够智能,无法做到这一点。
C# 类型推断只看方法签名。
Generic constraints are not part of the signature .
由于 T
没有直接在签名中使用,编译器不会推断它。
关于c# - 尽管 intellisense 列出了定义,但找不到定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13362267/
我现在选择Objective-J Cappuccino开发的编辑器是Sublime Text2。不幸的是,我没有找到Objective-J智能感知自动完成插件的运气。似乎应该可行,因为Objectiv
我想在 VSCode 中使用代码片段。但是,当我在 javascript 文件中键入 for 时,Intellisense 不会建议片段“for-loop” - 我需要手动滚动下拉列表并选择它。有没有
我正在 VisualStudio 中编写一个 dll。该库包含两个名称相同但 namespace 不同的类。 public namespace XCharting { public class
我们正在使用 MVVM Light Toolkit(来自 Galasoft - Laurent Bugnion)。 到目前为止,我们使用的是 Visual Studio 2010。 一切都运行良好(感
我们目前正在 Blend 3 中构建 WPF 解决方案,Intellisense 非常慢。 Visual Studio 2008 和 2010 beta 2 没有任何问题,Blend 2 也没有任何问
我正在为 Visual Studio Code 中的自定义语言编写扩展。语法检测通过 tmLanguage 文件运行良好。我试图弄清楚如何添加智能感知并转到符号支持,我既无法找到清晰的文档,也无法找到
/// <reference path="jquery-1.5.vsdoc.js" /> // IntelliSense works here. function ($, window, doc
我正在尝试为示例 Backbone.js 项目安装 TypeScript 定义。 在项目目录中,我发出了以下命令: npm init npm install typings typings insta
我最近通过 Web 平台安装程序安装了 ASP.NET MVC 3。我在 Razor View 中没有对 ViewBag 的智能感知支持。 Intellisense 适用于 Razor View 中的
我不想在 Visual Studio Code 中对 Markdown 文件进行单词补全,如何禁用它?理想情况下,仅适用于 Markdown,但在最坏的情况下,即使是全局切换也会很好。 最佳答案 VS
我有 VS 2012 的 RTM 版本,它很棒 - 但是 CSS 编辑器中的 VS11 中的智能感知不建议参数信息,但是......其他的东西,我不太确定那实际上是什么,它看起来像一些样本。 需要明确
我在 js 文件中添加了 vsdoc jquery 引用: /// 这工作得很好,但是一旦我在一个无冲突的包装器中写入内部... (function ($) { ...here... })(jQue
烦人的废话。我正在使用 VS 2012;我安装了 ReSharper 和 StyleCop (SC)。如果在一行代码上启用了 StyleCop 警告,除非修复它,否则我无法摆脱它。我想稍后处理 SC
我在 PPT 2013 中的“彩色参数帮助”功能有问题。 http://visualstudiogallery.msdn.microsoft.com/dbcb8670-889e-4a54-a226-a
在 javascript 上下文中查看异步模块定义 (AMD) 后,我想知道如何在 Visual Studio 2010 中为依赖模块获取智能感知。 例如给定模块A: define(function(
我有一个问题:我想将我的 Visual Studio 2012 的 UI 设置为默认(英语)语言,但我还希望有关 .NET 类型的 IntelliSense 提示以另一种语言显示。在 Visual S
可以去Resharper -> Options -> IntelliSense -> General吗并使用 Resharper 5 中的命令从 Resharper 切换到 Visual Studio
使用 Visual Studio 2015 Community edition 它会针对以下内容标记错误: { "type": "Microsoft.Compute/virtualMachi
我无法在Visual Studio 2017中找到该选项。我知道您可以更改正在编写的代码的字体,但是看不到IntelliSense的选项使我认为这是不可能的。 最佳答案 工具–选项–环境–字体和颜色>
我看过相关问题 Intellisense for custom config section problem with namespaces它描述了我遇到的同样问题。虽然解决方案对我不起作用。 我在这
我是一名优秀的程序员,十分优秀!