开发者

How to remove a property from class at run time

开发者 https://www.devze.com 2023-03-02 19:01 出处:网络
I开发者_如何学Cs it possible to remove a property from class at runtime, like: public Class A { public int num1 {get;set;}

I开发者_如何学Cs 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?


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.


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


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.


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");


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.


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.


//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();
0

精彩评论

暂无评论...
验证码 换一张
取 消