开发者

How can i extract value of properties from anonymous class?

When i declare

object o = new { name = "Bruce",Age=21 };
Console.WriteLine("name={0},age={1}"开发者_运维知识库,???,??? );

Now how can i print value of name and age?


Dont assign to an object variable, use var:

var o = new { name = "Bruce", Age = 21 };
Console.WriteLine( "name={0},age={1}, o.name, o.Age );


While not accessing the properties directly (see LBushkin's answer). ToString() is overloaded to list the content of all of the properties

var o = new { name = "Bruce", Age = 21 };
Console.WriteLine(o);// { name = Bruce, Age = 21 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜