Is it possible to remove a property from class at runtime, like:
public Class A
{
public int num1 {get;set;}
public int num2 {get;set;}
public int num3 {get;set;}
}
Class A Obj = new A();
At run time I want to remove num2
from obj
. Is it possible?
What you want to remove is called a property, not an attribute. No, it's not possible, so you should perhaps ask about what it is that you are trying to accomplish, instead of asking about the method that you thought that you could use to solve it.
Remove Property from an ObjectThe delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.
You can't remove a property, unless you remove it permanently, for all cases. What you can do, however, is to create multiple classes, in a class hierarchy, where one class has the property and the other hasn't.
The Java. util. Properties. clear() method is used to remove all the elements from this Properties instance.
This can't be done. Once compiled, a class definition is set.
As others said already, it's not possible.
Instead you can add another property e.g.
public List<string> ignoredProperties {get; set;}
Then at runtime add num2
to that list and check it for properties you should ignore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With