Link to home
Start Free TrialLog in
Avatar of gr8gonzo
gr8gonzoFlag for United States of America

asked on

C# Extending Classes and Overriding Types

I have a scenario where I am working with a 3rd party library that I cannot change, and I'm trying to figure out a way to work with some of their types. Basically, they have the following classes:
public class Food
{
    ... properties that apply to all food ...
}
public class Pie : Food
public class Grapefruit : Food
{
   public int NumberOfSeeds = 50;
}
public class Cherry : Food
{
   public int NumberOfSeeds = 1;
}

Open in new window

Now, "Grapefruit" and "Cherry" both have the "NumberOfSeeds" property, but that property is not part of the Food class.

I'm working on a "helper" class that removes seeds, so I'm trying to do something like this:
public class FoodHelper
{
  public Food item;

  public void RemoveSeeds()
  {
    item.NumberOfSeeds = 0;
  }
}

public class GrapefruitHelper : FoodHelper
{
  new public Grapefruit item;
}

public class CherryHelper : FoodHelper
{
  new public Cherry item;
}

Open in new window

I'm trying to avoid having to redefine "RemoveSeeds" over and over again in the child classes (the real method is pretty monstrous), but when I define it in the parent class, I get a warning that "Food" doesn't contain the "NumberOfSeeds" property, which is correct.

Also, if I try to use any valid type like "Cherry" instead of "Food" in the parent class in order to make the RemoveSeeds work, then I'm forced to override / hide the "item" in the child classes and the parent methods don't even see the right "item".

I've experimented with trying to use dynamic <T> types in my class definitions, but with no luck. Can anyone point me in the right direction here?
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gr8gonzo

ASKER

To be clear, all the code is owned by my company, but the problem is in a portion of the code developed by another department, and it cannot be resolved without going through another cycle that takes too long for my current project task. I actually do have access to the source code, but cannot just compile my own version of that class without bringing along a thousand dependencies. I am only writing a plugin for this product, so that's the predicament.

I was hoping to avoid Reflection for the performance hits you mentioned. Is there a way to define a -new- type "group" like "Fruit" that tells .NET that "This parameter called Fruit will always be either Cherry or Grapefruit?"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gave all the points to kaufmed for the help, but the ultimate answer here was a different approach to the code.