开发者

how to add properties to objects dynamically in c#

开发者 https://www.devze.com 2023-01-12 03:53 出处:网络
I want to add the properties to the class / object dynamically in 开发者_如何学运维c# in VS 3.5. how can i do this?If you want to create an object that supports run-time addition of properties, and yo

I want to add the properties to the class / object dynamically in 开发者_如何学运维c# in VS 3.5. how can i do this?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消