Jak działa ToString w anonimowym typie?
Bawiłem się anonimowymi typami i przypadkowo wypuściłem go na konsolę. Wyglądało to zasadniczo tak, jak je zdefiniowałem.
Oto krótki program, który go odtwarza:
using System;
class Program
{
public static void Main(string[] args)
{
int Integer = 2;
DateTime DateTime = DateTime.Now;
Console.WriteLine(new { Test = 0, Integer, s = DateTime });
Console.ReadKey(true);
}
}
Teraz wyjście:
{ Test = 0, Integer = 2, s = 28/05/2013 15:07:19 }
Próbowałem użyć dotPeek, aby dostać się do zespołu, aby dowiedzieć się, dlaczego, ale to nie pomogło.[1] Oto kod dotPeek'd:
// Type: Program
// Assembly: MyProjectName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// Assembly location: Not telling you! :P
using System;
internal class Program
{
public static void Main(string[] args)
{
Console.WriteLine((object) new
{
Test = 0,
Integer = 2,
s = DateTime.Now
});
Console.ReadKey(true);
}
}
Tak niewiele się różni.
Jak to działa? Jak to wyprowadza?
Uwagi:
[1]: Zapomniałem włączyć opcję „Pokaż kod wygenerowany przez kompilator”, dlatego nie udało mi się go uruchomić.