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?

5
  • This seems like an awfully complicated way to do the same thing that a plain old Dictionary can do anyway. Commented Feb 10, 2017 at 9:14
  • 1
    I think you should look for a dynamic object. Have a look at stackoverflow.com/questions/12709333/… Commented Feb 10, 2017 at 9: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. Commented Feb 10, 2017 at 9:39
  • Can any of you please provide a code sample? Commented Feb 10, 2017 at 16:22
  • Possible duplicate of How Can I add properties to a class on runtime in C#? Commented Apr 2, 2017 at 22:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.