How to remove a property from a class at runtime

Is it possible to remove a property from the class at runtime, for example:

public Class A { public int num1 {get;set;} public int num2 {get;set;} public int num3 {get;set;} } Class A Obj = new A(); 

At runtime, I want to remove num2 from obj . Is it possible?

+6
source share
4 answers

It's impossible. After compilation, the class definition is specified.

+7
source

As others have said, this is not possible.

Instead, you can add another property, for example.

 public List<string> ignoredProperties {get; set;} 

Then, at run time, add num2 to this list and check it for properties that you should ignore.

+1
source

You need to come up with a Model / ViewModel approach. Create a ViewModel that will have limited properties for your requirement.

0
source

I agree with Nic's answer: This is not possible. After compilation, the class definition is specified.

But you can create a class property dynamically, what you want, by reflection.

0
source

Source: https://habr.com/ru/post/887338/


All Articles