How to change class definition at runtime?

Is there a way to change the properties that a class has (add / remove properties) at runtime?

+4
source share
5 answers

You cannot do this if you are not working with an instance ExpandoObject. Metadata for type CLR is committed to the assembly and cannot be changed at run time. If you really need this dynamic behavior, you should use a dynamic type (e.g. EpandoObject) that supports this behavior.

+10
source

Andrew Hare : # 4 .NET 4 DynamicObject , . DynamicObject , , , , . . , / , ExpandoObject.

ExpandoObject . ,

+4

, System.Reflection.Emit -

+2

To develop a user interface (that is, what is presented to the end user), consider implementing ICustomTypeDescriptor (in System.ComponentModel). Many controls are aware of this interface and will use it to request properties that an instance or type provides.

+1
source

If you are on 3.5, you can use IL to create a dynamic type, and also complete the task, but it is much more complicated, but there are some frameworks for this, I suppose.

0
source

All Articles