-1

Say I have these 2 classes

class Car {
  public string CarName {get;set;}
}

class EmployeeCar:Car {
  public string EmployeeName {get;set;}
}

When I call this API

[HttpGet]
public Car GetCar(Employee employee) {
  return GetEmployeeCar(employee);
}

assuming

private EmployeeCar GetEmpoyeeCar(Employee employee) { 
    return new EmployeeCar { CarName: "Car 1", EmployeeName: "Employee 1" };
}

I am getting this

{ CarName: "Car 1", EmployeeName: "Employee 1" }

Note that EmployeeName does not belong to Car.

How can I get the API to return only the properties of Car? (which is the return type of the API) ie.

{ CarName: 'Car 1' }

SOLUTION

This is much longer than I hoped (not sure if there is a shorter version) but I hope this can help someone

public Car GetCar(Employee employee) {
    Car carDirty = GetEmployeeCar(employee); // { CarName: "Car 1", EmployeeName: "Employee 1" }

    Car carClean = SweepForeignProperties(carDirty); // Only keep properties of Car

    return carClean; // { CarName: "Car 1" }
}

/// <summary>Only keep T's own properties, getting rid of unknown/foreign properties that may have come from a child class</summary>
public static T SweepForeignProperties<T>(T dirty) where T: new()
{
    T clean = new T();
    foreach (var prop in clean.GetType().GetProperties())
    {
        if (prop.CanWrite)
            prop.SetValue(clean, dirty.GetType().GetProperty(prop.Name).GetValue(dirty), null);
    }
    return clean;
}
Aximili
  • 28,626
  • 56
  • 157
  • 216
  • have a [look at this](https://stackoverflow.com/a/1875702) – boop_the_snoot Aug 18 '17 at 07:23
  • If your question pertains to more than just plain C# inheritance, you need to provide more context. The marked duplicate addresses the former (and boils down to "what? are you crazy?"). If you have a serialization scenario where you think this would be more reasonable to do, you need to post a new question which is more specific and provide a lot more details about why and how you intend to use this. – Peter Duniho Aug 19 '17 at 01:22
  • @PeterDuniho and Nobody Thanks but that was one of the first results I found googling it. Obviously you guys didn't read my question, only the title. May be my title was wrong, let me edit it. Does it make it clearer? – Aximili Aug 21 '17 at 00:07
  • _"Does it make it clearer?"_ -- nope, not even a little bit. A big part of the problem is that your question offers no [mcve] to show what the problem is, nor any explanation of what you tried to fix it. If you want an API to return an object that has only the `CarName` property, then don't return an object that also has the `EmployeeName` property. For example, copy the data you _do_ want into a new object of the type you _do_ want. Trying to _remove_ properties from objects is just futile. Saying they don't "belong" to the class when they clearly do is just plain confusing. – Peter Duniho Aug 21 '17 at 00:10

1 Answers1

0

If you are using Newtonsoft Json, you can add the JsonIgnore attribute which causes the marked property from being serialized/deserialized

AcidJunkie
  • 1,878
  • 18
  • 21
  • Thanks AcidJunkie. That may work in some cases, but I need EmployeeCar properties to be serializable somewhere else. I have found a solution though :) – Aximili Aug 21 '17 at 01:01