0

How to add property in a class on run time and when I run the code

ClassName.GetType().GetProperties();

it will give me the name of property which i have added.

ni3.net
  • 377
  • 4
  • 14
  • possible duplicate of [How do I create dynamic properties in C#?](http://stackoverflow.com/questions/947241/how-do-i-create-dynamic-properties-in-c) – nawfal Jul 20 '14 at 06:47

1 Answers1

0

You can use Expando Objects. They allow to add and remove properties on run time.

The ExpandoObject class belongs to the System.Dynamic namespace and is defined in the System.Core assembly. ExpandoObject represents an object whose members can be dynamically added and removed at run time. The class is sealed

Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • @Ehsan.. but when i use GetType().GetProperties() it won't give me the name of property that i added dynamically – ni3.net Mar 28 '14 at 07:33
  • @ni3.net You can. Take a look here of how you can achieve that. http://stackoverflow.com/questions/16567283/exposing-properties-of-an-expandoobject – Ehsan Mar 28 '14 at 07:35
  • @ni3.net Or if you know the properties that you want to add to the dynamic object. There is a simpler solution of creating dynamic object as well. – Ehsan Mar 28 '14 at 07:39