Non-read only alternative to anonymous types

前端 未结 6 1519
误落风尘
误落风尘 2020-11-29 09:02

In C#, an anonymous type can be as follows:

method doStuff(){
     var myVar = new {
         a = false, 
         b = true
     }

     if (myVar.a) 
     {         


        
6条回答
  •  清歌不尽
    2020-11-29 09:38

    You won't be able to get the nice initialization syntax but the ExpandoObject class introduced in .NET 4 would serve as a viable solution.

    dynamic eo = new ExpandoObject();
    
    eo.SomeIntValue = 5;
    eo.SomeIntValue = 10; // works fine
    

提交回复
热议问题