13

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?

Servy
  • 202,030
  • 26
  • 332
  • 449
KhanZeeshan
  • 1,410
  • 5
  • 23
  • 36
  • What you want to remove is called a property, not an attribute. – Anton Tykhyy May 05 '11 at 07:08
  • 1
    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. – Guffa May 05 '11 at 07:15
  • What exactly are you trying to do here? Are you sure you want to remove the num2 property entirely (breaking all other code that uses it?). Or perhaps you just dont want it to show up in intellisense... ? – MattDavey May 05 '11 at 07:51

8 Answers8

12

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

Nik
  • 7,113
  • 13
  • 51
  • 80
1

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.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
1

I had a very exact use case. In my case, I want to ignore some properties when posting the data model to ODATA via Json. This property may not be a table field so I want to ignore while serializing this to JSON. I achieved this by below steps.

  • I used the DataAnnotations to decorate that attribute as [ReadOnly(true)]
  • I then created a custom JsonSerializer from ISerializer to ignore the ReadOnly Properties like below:
public string Serialize(object obj)
{
    return JsonSerializer.Serialize(obj,
           new JsonSerializerOptions
           {
               IgnoreReadOnlyProperties = true
           });
}

This solved my problem to ignore some properties that I don't want to pass via Json/OData calls or any Api/endpoint wherever it is.

Ak777
  • 346
  • 7
  • 18
0

You have to comeup with Model/ViewModel approach. Create a ViewModel which will have limited properties for your requirement.

Kurkula
  • 6,386
  • 27
  • 127
  • 202
0

I agree with Nic reply: This can't be done. Once compiled, a class definition is set.

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

Ujjwal Jha
  • 79
  • 2
  • 13
0

My case was much easier

I have a class which is POST
then I need to remove few properties and save this to JSON
I did go around with System.Dynamic.ExpandoObject copy the class

            Object value;
            
            System.Dynamic.ExpandoObject cloneData = JsonSerializer.Deserialize<ExpandoObject>(JsonSerializer.Serialize(data));
           
            cloneData.Remove("IP", out value);
            value = value;
            cloneData.Remove("analytics", out value);
            value = value;


            string azurecontainer = @"data";
            string azureblobJSONDataFilename = @"profile/" + _userInfoSessionB.u + @".json";
            string JSONData = JsonSerializer.Serialize(cloneData);
            object p = azureStorage.UploadBlob2ContainerTextAsync(JSONData, azurecontainer, azureblobJSONDataFilename, "application/json", "public, max-age=30");
Valentin Petkov
  • 1,570
  • 18
  • 23
0

I was not able to REMOVE the property, I was trying to create a dynamic JSON, with 2 different classes merged together but without some properties (not needed for that merged class), so what I did was, I added a custom attribute and added to field/properties which I didn't need, and used reflection to create a custom JSON at runtime after merging 2 classes.

KhanZeeshan
  • 1,410
  • 5
  • 23
  • 36
0
//Convert your object to JObject
var jsonDoc = JObject.FromObject(doc);
//Select the Property To Remove
var PropertyToRemove= jsonDoc.Property("PropertyToRemove");
// Remove the Property
sig.Remove();