匿名类型是不可变的,结构的还是完整的对象?

时间:2012-01-01 13:37:23

标签: c# .net dynamic anonymous-types

set value of a property of an anonymous type using reflection / TypeDescriptor is it possible?

根据@Slaks,

  

C#匿名类型是不可变的,它们的属性不可能   改变。

示例:

dynamic p  = new {aaa="1", bbb="2"};

我的问题是,什么是不可变的? (结构,价值观,整个世界?)

结构
{ 
          something (in type of string) 
          ,
         something (in type of string ) 
    } 

(意思是 - 结构是不可变的 - 我不能改变它的结构)

 { 
          something called aaa (in type of string) 
          ,
         something called bbb (in type of string ) 
    } 

整个世界:

  { 
      something (in type of string + value of 1) 
      ,
     something (in type of string + value of 2) 
} 

1 个答案:

答案 0 :(得分:2)

不可变意味着它无法改变。

结构值,在这种情况下。

p的类型将始终为两个string属性 - 一个名为aaa,值为"1",另一个名为bbb,其值为"2" {{1}}。

您无法将成员添加到此匿名类型,也无法更改属性的值。