简体   繁体   English

类型名称是“ _Closure $ __ 1”是什么意思?

[英]What does it mean when a Type name is “_Closure$__1”?

I have a method that I'm using to output all the class names in an Assembly: 我有一种方法用于在程序集中输出所有类名:

private static void ListClasses()
{
    var assembly = Assembly.LoadFile(@"path\to\my.dll");
    Type[] types = assembly.GetTypes().Where(t => t.IsClass).ToArray();

    using (StreamWriter w = File.AppendText("log.txt"))
    {
        foreach (var type in types)
        {
            w.WriteLine(type.Namespace + "," + type.Name);
            w.Flush();
        }

        w.Close();
    }

    Console.WriteLine("Done");
}

The only problem I'm seeing is some of the class names have this output: 我看到的唯一问题是某些类名称具有以下输出:

The.Namespace,_Closure$__1

The last number will increment each time the Type name needs to be output this way. 每当需要以这种方式输出类型名称时,最后一个数字将递增。 Can anyone shed some light as to what this means? 任何人都可以阐明这意味着什么吗?

Those are compiler generated classes used by lambda expressions to capture free variables in the lambda expression. 这些是由lambda表达式使用的编译器生成的类,用于捕获lambda表达式中的自由变量。

http://msdn.microsoft.com/en-us/library/bb981314%28v=vs.80%29.aspx http://msdn.microsoft.com/zh-cn/library/bb981314%28v=vs.80%29.aspx

A closure is when a local variable is persisted beyond its scope. 闭包是指局部变量超出其范围而持续存在的情况。 For example: 例如:

public IEnumerable<Employee> GetEmployees(string lastName)
{
    return employees.Where(e => e.LastName == lastName);
}

Compiling this will result in closure, where the lastName will keep its value behind the scenes until the Where is evaluated. 编译此代码将导致关闭,在此位置, lastName将在幕后保持其值,直到对Where求值为止。

For more information: http://en.wikipedia.org/wiki/Closure_(computer_science ) 有关更多信息: http : //en.wikipedia.org/wiki/Closure_(computer_science

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM