繁体   English   中英

C#类型的尾随点表示什么?

[英]What does the trailing dot on a C# type indicate?

我一直在查看与Razor View引擎相关的调试器中的一些代码,我注意到一些类型出现在Debugger中,在类型名称的末尾有一个尾随点字符,例如:

{Nancy.ViewEngines.Razor.RazorViewEngine。}

有谁知道这表明了什么? 在对象上指定强制转换时使用它是无效的语法,因此我对它在调试器中的含义感到好奇。

编辑 :根据@Damien_The_Unbeliever的要求,调试器中变量的屏幕截图:

调试图像

我正在看的代码:

public TCompiledView GetOrAdd<TCompiledView>(
            ViewLocationResult viewLocationResult, Func<ViewLocationResult, TCompiledView> valueFactory)
        {
            TCompiledView compiledView = default(TCompiledView);
            compiledView = (TCompiledView)this.cache.GetOrAdd(viewLocationResult, x => valueFactory(x));

为了给出更多背景信息,我们尝试将日志记录添加到我们的Nancy View Cache中,以调查Razor Views抛出编译错误的间歇性问题,但这与问题无关。

我已经看到这种情况发生在变量/值实际上是编译器生成的类型时(例如,用于保存由lambda,async,iterator等捕获的“局部变量”)。 调试器(在各个地方)似乎无法显示实际的类名。


例如这个示例程序:

class Program
{
    static void Main(string[] args)
    {
        var p = new Program();
        p.DoStuff();
    }

    void DoStuff()
    {
        int i = 19;
        Expression<Func<int>> j = () => i + 10;
        var k = (((j.Body as BinaryExpression).Left as MemberExpression).Expression as ConstantExpression).Value;
        Console.ReadLine();
    }
}

使用Console.ReadLine()上的断点,您会发现k类的类型看起来像Program. 而不是Program+<>_DisplayClass0


Jeppe的补充:这个例子略微简化了上面的内容,避免了表达式树。 查看委托实例的Target ,它将是生成的类的实例。 为了比较,还要查看迭代器块类型:

using System;
using System.Collections.Generic;

static class Program
{
  static void Main()
  {
    int i = 19; // to be captured by lambda, will become field on a generated class
    Func<int> f = () => i;
    var target = f.Target;  // when debugging type looks like "Program."
    Console.WriteLine(target.GetType().ToString()); // writes "Program+<>c__DisplayClass1"

    var seq = GetSeq();  // when debugging type looks like "Program.GetSeq"
    Console.WriteLine(seq.GetType().ToString()); // writes "Program+<GetSeq>d__3"
  }

  static IEnumerable<int> GetSeq() // returns "state machine" (iterator block)
  {
    yield return 42;
  }
}

暂无
暂无

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

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