Convert an existing type to an anonymous type and add a new property

Is it possible to take an existing type and create an anonymous type from it with additional properties? Example (in VB)

Public Class Person
  Public Name As String
  Public Age As Integer
End Class

Dim p As New Person With {.Name = "James", .Age = 100}

Dim n = New With {.ShoeSize = 10}

I want the second object (n) to clone all the properties of p, and then add a new property (ShoeSize).

Is it possible?

Many thanks

James

+3
source share
2 answers

There is no syntax in C #. You will need to create an anonymous type with all properties.

+2
source

If you need to do this regularly, modifying my extensions listed in here can save some typing.

, string.Join(", " , from p in ps select "." + p.Name + " = " + VarName + "." + p.Name), .

OP: p.AllFieldsVb("p") ".Name = p.Name, .Age = p.Age".

0

Source: https://habr.com/ru/post/1702466/


All Articles