Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a property from class at run time

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?

like image 928
KhanZeeshan Avatar asked May 05 '11 07:05

KhanZeeshan


People also ask

Can we remove a property from a class?

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.

How do I remove a property from an object?

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.

How to remove a property in c#?

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.

How do I remove a property from a Java object?

The Java. util. Properties. clear() method is used to remove all the elements from this Properties instance.


2 Answers

This can't be done. Once compiled, a class definition is set.

like image 193
Nik Avatar answered Oct 26 '22 08:10

Nik


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.

like image 21
Shadow Wizard Hates Omicron Avatar answered Oct 26 '22 08:10

Shadow Wizard Hates Omicron