How Can I add properties to a class on runtime in C#?

前端 未结 3 514
梦如初夏
梦如初夏 2020-12-01 18:46

I have a class :

class MyClass 
{
}
...
MyClass c = new MyClass();

Is it possible to add properties / fields to this class on run-time ?

3条回答
  •  萌比男神i
    2020-12-01 19:00

    Yes, you can use the ExpandoObject class to achieve this.

    dynamic expando = new ExpandoObject();
    expando.Prop1 = 10;
    expando.Prop900 = string.Empty;
    

提交回复
热议问题