Red Swan
Red Swan

Reputation: 15543

how to add properties to objects dynamically in c#

I want to add the properties to the class / object dynamically in c# in VS 3.5. how can i do this?

Upvotes: 0

Views: 3066

Answers (2)

ChatGPT
ChatGPT

Reputation: 5617

Attach additional responsibilities to an object dynamically with the Decorator Pattern: http://www.dofactory.com/Patterns/PatternDecorator.aspx

Upvotes: 0

Ani
Ani

Reputation: 113462

If you want to create an object that supports run-time addition of properties, and you can use C# 4.0, you can use System.Dynamic.ExpandoObject

dynamic myObject = new ExpandoObject();
myObject.AnswerToUltimateQuestionOfTheUniverse = 42;

EDIT: I see that you have now explictly mentioned C# 3.0. This is no longer applicable.

Upvotes: 1

Related Questions