- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 JetBrains Rider 进行 C# 编程,我猜在使用 ReSharper 时也会出现此警告:
我写了这个函数,GetTicket
:
public async Task<IEnumerable<Ticket>> GetTicket(int id)
{
return await _memoryCache.GetOrCreateAsync(_cachingFunctionalty.BuildCachingName(id), entry =>
{
entry.SlidingExpiration = TimeSpan.FromSeconds(10);
return GetTicket_uncached(id);
});
}
和GetTicket_uncached
,它称之为:
private async Task<IEnumerable<Ticket>> GetTicket_uncached(int id)
{
RestClient client = new RestClient(ServiceAdress);
Request req = new Request
{
Method = Method.GET,
Resource = "api/tickets/get",
Parameters = new {ident = id}
};
return await client.ExecuteRequestAsync<Ticket[]>(req);
}
所以,参数id
在方法中public async Task<IEnumerable<Ticket>> GetTicket(int id)
突出显示并带有以下警告:
Closure allocation: 'id' parameter and 'this' reference
我在谷歌搜索时发现了一些东西,但我仍然不知道这意味着什么,有什么问题?
最佳答案
此消息来自堆分配查看器插件。当您创建要作为 Func
传递给 GetOrCreateAsync
的 lambda 时,您正在从调用方法 (GetTicket
) 中捕获一些值,并且稍后使用它们。
编译此代码时,编译器会将此 lambda 重写为一个包含值的类以及一个主体与 lambda 主体相同的方法,尽管它会使用在这个新类中捕获的值,并且不是原始方法调用。
Heap Allocations Viewer 插件所说的是在运行时这里发生了一个隐藏的分配——正在分配这个新的编译器生成的类、分配的值和调用的方法。
插件告诉您 id
正在这个新类中被捕获和分配 - 这在 lambda 中很明显,因为您在代码中看到了它。但是您还捕获了 this
,因为 GetTicket_uncached
是实例方法而不是静态方法。您不能在没有 this
的情况下调用实例方法,因此 id
和 this
都被捕获并分配到编译器生成的类中。
您无法摆脱 id
变量的分配,但是如果您使 GetTicket_uncached
可以摆脱 this
引用> static(但这可能需要传入 ServiceAddress
,在这种情况下,Heap Allocations Viewer 会告诉您闭包分配现在是 id
和 ServiceAddress
)。
您可以在 ReSharper help page for the "Implicitly capture closure" warning 中查看更多详细信息.虽然它讨论了不同的场景和警告消息,但有关分配类以捕获变量的背景详细信息很有用。
关于c# - 闭包分配的意义和问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49862819/
以下闭包函数在 javascript 中运行良好。 function generateNextNumber(startNumber) { var current = startNumber;
Swift的闭包(Closures)是一种将功能块和上下文整合并演示在代码中的一种手段。闭包可以捕获并存储其上下文中的变量和常量。与普遍存在于其他语言的匿名函数(如Python的lambda、Java
在本教程中,您将借助示例了解 JavaScript 闭包。 在了解闭包之前,您需要了解两个概念: 嵌套函数 返回函数 JavaScript 嵌套函数 在 JavaScript 中,一个函数也可
在本教程中,您将借助示例了解 JavaScript 闭包。 在了解闭包之前,您需要了解两个概念: 嵌套函数 返回函数 JavaScript 嵌套函数 在 JavaScript 中,一个函数也可
闭包介绍 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现。 要理解闭包,首先必须理解Javascript特殊的变量作用域。 1.全局变量和局部变
这个问题已经有答案了: Methods in ES6 objects: using arrow functions (6 个回答) 已关闭 6 年前。 我已经在 stackoverflow 上到处查找
这个问题已经有答案了: How do JavaScript closures work? (86 个回答) 已关闭 9 年前。 我有一个关于 Javascript 闭包的简单问题: 给出了以下函数:
所以我有以下内容: Object a = data.getA(); Object b = data.getB(); Object c = data.getC(); // and so on 这些对象是
现在已经很晚了,我大脑中道格拉斯·克罗克福德居住的部分已经关闭。我尝试了一些方法,但没有达到预期效果。 我有一个 Canvas ,我在其中画了两条线,然后在计时器上淡出它们,但只有循环中的最后一行淡出
因此,我创建了一个变量 car,然后将其分配给一个函数并添加了参数模型、年份。然后在函数内引用参数创建一个对象。 然后创建“闭包”内部函数 yourCar() 并返回其中的外部函数对象“Propert
我正在 Mozilla 开发者网站上阅读关于关闭的解释,并且有点挣扎。请查看 Mozilla 网站上的以下代码。我有点理解它是如何工作的,但我认为我的评论下面的代码也应该工作。为什么一点击18、20就
这个问题在这里已经有了答案: UnboundLocalError trying to use a variable (supposed to be global) that is (re)assig
以下程序返回“本地”,根据我正在阅读的教程,它旨在演示闭包现象` 我不明白的是,为什么最后为了调用parentfunction,将其分配给变量“child”,然后调用“child”。 为什么只写 pa
我读到闭包末尾的()会立即执行。那么,这两者之间有什么区别。我在一些代码中看到了第一个用法。 谢谢。 for (var a=selectsomeobj(),i=0,len=a.length;i
代码如下 var collection = (function (){ var x = 0; return {
我仍然对 JavaScript 中的闭包概念感到困惑。我明白闭包是内部函数在母函数返回后访问在其母函数中创建的变量的能力。但是我仍然很困惑,如果我们可以在函数内部创建一个变量,为什么我们必须创建内部函
我搜索了很多主题并没有找到答案,或者问题太复杂了。所以没关系。这是我的第一个问题。 这是 SQL SELECT parent.*, ( SELECT COUNT(*) FROM
有 JS 高手可以解释为什么会这样吗: $$={} (function(x){ x.newModule = { func: function(){...} };
在此示例中,我尝试按值传递,但传递的是引用。 for (int i = 0; i new PhoneJobTest(i); t.Start(); } 这可以像这样补救: for (int
从 $.each() 中访问 this.rules 变量的最佳方式是什么?任何关于原因/方式的解释也会有帮助! app.Style = function(node) { this.style
我是一名优秀的程序员,十分优秀!