我想知道我是否可以在 C# lambda 中使用 this 关键字,虽然实际上我知道我可以,但我想确保这不是坏事或以后会产生微妙的问题。
已阅读 variable scope for lambdas 上的规则,我可以看到:
A variable that is captured will not be garbage-collected until the delegate that references it goes out of scope.
所以这让我假设对象实例 (this) 也将被捕获。为了对此进行测试,我编写了这个人为设计的示例,这是我在实际代码中大致想要实现的目标 - 用 LINQPad 编写,因此我调用了 Dump() 方法:
void Main()
{
Repository repo = new Repository();
Person person = repo.GetPerson(1);
person.ID.Dump("Person ID - Value Assigned");
person.Name.Dump("Person Name - Lazily Created");
}
class Person
{
public Person(Lazy<string> name)
{
this.name = name;
}
public int ID { get; set; }
private Lazy<string> name;
public string Name
{
get { return name.Value; }
}
}
class Repository
{
public Person GetPerson(int id)
{
// Setup person to lazily load a name value
Person person = new Person(
new Lazy<string>(
() => this.GetName() // <--- This I'm not sure on...
)
);
person.ID = id;
return person;
}
public string GetName()
{
return "John Smith";
}
}
这会运行并为我提供正确的输出,因此从 lambda 中访问 this 显然有效。我想检查的是:
this 引用一直保存在内存中,直到不再使用 lambda 为止?从我的小实验来看似乎是这样,但如果有人能提供更多细节,我会很感兴趣。最佳答案
使用this没有错在 lambda 中,但正如您提到的,如果您确实使用 this (或者如果你隐式地使用它,通过调用任何非静态成员函数或使用非静态成员变量)那么垃圾收集器将保留 this 的对象。指的是至少只要代表还活着就还活着。由于您将 lambda 传递给 Lazy ,这意味着 Repository至少只要 Lazy 就会活着对象还活着(即使您从未调用过 Lazy.Value )。
为了稍微揭开它的神秘面纱,它有助于查看反汇编程序。考虑这段代码:
class Foo {
static Action fLambda, gLambda;
int x;
void f() {
int y = 0;
fLambda = () => ++y;
}
void g() {
int y = 0;
gLambda = () => y += x;
}
}
标准编译器将其更改为以下内容(尝试忽略 <> 额外的尖括号)。如您所见,使用函数体内变量的 lambda 被转换为类:
internal class Foo
{
private static Action fLambda;
private static Action gLambda;
private int x;
private void f()
{
Foo.<>c__DisplayClass1 <>c__DisplayClass = new Foo.<>c__DisplayClass1();
<>c__DisplayClass.y = 0;
Foo.fLambda = new Action(<>c__DisplayClass.<f>b__0);
}
private void g()
{
Foo.<>c__DisplayClass4 <>c__DisplayClass = new Foo.<>c__DisplayClass4();
<>c__DisplayClass.<>4__this = this;
<>c__DisplayClass.y = 0;
Foo.gLambda = new Action(<>c__DisplayClass.<g>b__3);
}
[CompilerGenerated]
private sealed class <>c__DisplayClass1
{
public int y;
public void <f>b__0()
{
this.y++;
}
}
[CompilerGenerated]
private sealed class <>c__DisplayClass4
{
public int y;
public Foo <>4__this;
public void <g>b__3()
{
this.y += this.<>4__this.x;
}
}
}
如果您使用 this ,无论是隐式还是显式,它都成为编译器生成的类中的成员变量。所以类 f() , DisplayClass1 , 不包含对 Foo 的引用,但是 g() 的类, DisplayClass2 , 确实。
如果 lambda 不引用任何局部变量,编译器会以更简单的方式处理它们。所以考虑一些稍微不同的代码:
public class Foo {
static Action pLambda, qLambda;
int x;
void p() {
int y = 0;
pLambda = () => Console.WriteLine("Simple lambda!");
}
void q() {
int y = 0;
qLambda = () => Console.WriteLine(x);
}
}
这次 lambda 不引用任何局部变量,因此编译器将您的 lambda 函数转换为普通函数。 p() 中的 lambda不使用 this所以它变成了一个静态函数(称为 <p>b__0 ); q() 中的 lambda确实使用 this (隐含地)因此它成为一个非静态函数(称为 <q>b__2 ):
public class Foo {
private static Action pLambda, qLambda;
private int x;
private void p()
{
Foo.pLambda = new Action(Foo.<p>b__0);
}
private void q()
{
Foo.qLambda = new Action(this.<q>b__2);
}
[CompilerGenerated] private static void <p>b__0()
{
Console.WriteLine("Simple lambda!");
}
[CompilerGenerated] private void <q>b__2()
{
Console.WriteLine(this.x);
}
// (I don't know why this is here)
[CompilerGenerated] private static Action CS$<>9__CachedAnonymousMethodDelegate1;
}
注意:我使用 ILSpy 查看编译器输出将选项“反编译匿名方法/lambdas”关闭关闭。
关于C# Lambdas 和 "this"变量作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11103745/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file