c#

  • The class of the object being serialized inherits from another class with some shared property names.
  • public class BaseModel { public string Title { get; set; } public decimal CleanUpHours { get; set; } public decimal InstallHours { get; set; } } public class DerivedModel : BaseModel { public new BucketHoursWithCalculations CleanUpHours { get; set; } public new BucketHoursWithCalculations InstallHours { get; set; } }
  • I am “shadowing” those properties in the derived class so that they can be a different type from their same-name counterpart in the base class.
  • The derived/shadowed properties were not serialized, and the base properties were, but only in the cases where there was a name conflict (for example, the Title property in the base model serialized just fine).
  • This is intentional, as I need the property names to match for some reflection that’s happening.

In my ASP.NET MVC web application, I am using the built in Controller.Json() method to serialize an object and send it back to the client in response to an AJAX call. The class of the object being serialized inherits from another class with some shared property names. This is intentional, as I need the property names to match for some reflection that’s happening. I am “shadowing” those properties in the derived class so that they can be a different type from their same-name counterpart in the base class. Here’s a simplified example:

@ng_real_ninja: How do I get only a shadowed property, not the base property, to get JSON serialized? …

method to serialize an object and send it back to the client in response to an AJAX call. The class of the object being serialized inherits from another class with some shared property names. This is intentional, as I need the property names to match for some reflection that’s happening. I am “shadowing” those properties in the derived class so that they can be a different type from their same-name counterpart in the base class. Here’s a simplified example:

Inspecting the object in Visual Studio before it gets serialized shows both the base and derived versions of those properties, as shown here (please excuse all the extra properties — my sample classes above are more simplified than what I’m actually using, but the principle is the same):

Here’s what that object looks like on the client once it’s serialized into JSON:

property in the base model serialized just fine).

is used by Entity Framework, and must have public properties. Any help would be appreciated.

c#

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.