1

I have an empty class.

public class Item
{
   //empty
}

I need to add properties to this class dynamically from PropertyInfo objects.

List<PropertyInfo> propertyInfo = Util.GetProperties();

Finally after adding properties, the class should be like:

public class Item
{
   //dynamically added properties
   public string Name {get;set;}
   public string Description{get;set;}
}

And I need to access the property from the class opbect like:

var item = new Item();
var name = item.Name;

Is this possible in c#? How can I implement this? Can I implement Property Changed notification to dynamically added properties?

Rahul
  • 2,431
  • 3
  • 35
  • 77
  • This seems like an awfully complicated way to do the same thing that a plain old `Dictionary` can do anyway. – Abion47 Feb 10 '17 at 09:14
  • 1
    I think you should look for a dynamic object. Have a look at http://stackoverflow.com/questions/12709333/how-to-create-own-dynamic-type-or-dynamic-object-in-c – Madenis Feb 10 '17 at 09:24
  • You cannot do this with user defined types. The thing you can do is to do that in compile time not runtime. Same thing applies to dynamic data types. They are created at runtime. – mrogal.ski Feb 10 '17 at 09:39
  • Can any of you please provide a code sample? – Rahul Feb 10 '17 at 16:22
  • Possible duplicate of [How Can I add properties to a class on runtime in C#?](http://stackoverflow.com/questions/14724822/how-can-i-add-properties-to-a-class-on-runtime-in-c) – Nate Barbettini Apr 02 '17 at 22:43

0 Answers0