b01
b01

Reputation: 4384

How can I add a new property to an instance of an anonymous type?

Is it possible to add a new property to an anonymous type instance after it has been initialized? I want to do something like the following:

// Initialize new object to hold subset of information about each transaction.
var transactionJson = new {};

transactionJson[transaction.Id] = new {
     Status = transactionList.StatusProperty,
     Completed = transactionList.Completed,
     First = transactionList.First
};

Upvotes: 0

Views: 356

Answers (1)

SLaks
SLaks

Reputation: 887413

That's inherently impossible.
Anonymous types are immutable, and are type-safe.

You should use a dictionary or a DynamicObject.

Upvotes: 3

Related Questions