7

Let's say that I'm using a library and I would like to add a property that doesn't exist to an existing class. In this case, I'd like to add Color as a property of Fruit.

namespace Library
{
    public class Fruit : System.IDisposable
    {
        public string Name { get; set; }

        public void Dispose() { }
    }
}

Is there any way to do that in C#?


Example that won't work

My first thought was partial classes but that would require that both classes would have the word partial in them. So that's not doable.

namespace Library
{
    public partial class Fruit
    {
        public Color Color { get; set; }
    }
}

This seems awful

I could inherit Fruit and create a MyFruit class. This seems clunky to me. I'd much rather call Person.Fruits.FirstOrDefault().Color than to have to create a MyPerson object that inherits from Person and then call MyPerson.MyFruits.FirstOrDefault().Color.

namespace Library
{
    public class MyFruit : Fruit
    {
        public Color Color { get; set; }
    }
}

namespace Library
{
    public class MyPerson : Person
    {
        public MyFruit MyFruit { get; set; }
    }
}
  • Your last example may be somewhat wrong: you don't have to subclass everything to add a property to one thing. Person.Fruits.OfType<MyFruit>().FirstOrDefault() should be all you need, rather than creating a hierarchy somewhere else. Something which takes Fruit naturally takes MyFruit. – Magus May 1 '15 at 22:22
11

The best answer is you shouldn't. But if you don't mind a performance overhead, you can do something similar with ConditionalWeakTable<,>:

public static class ExtraFruitProperties
{
    private static readonly ConditionalWeakTable<Fruit, Color> _colors = new ConditionalWeakTable<Fruit, Color>();

    public static void SetColor(this Fruit @this, Color color) 
    {
         _colors.Remove(@this);
         _colors.Add(@this, color);
    }

    public static Color GetColor(this Fruit @this) 
    {
         Color color;
         if(_colors.TryGetValue(@this, out color)) return color;
         return default(Color);
    }
}
  • 4
    This is indeed one of the major purposes for which ConditionalWeakTable was built; an important thing to note, however, is that some classes may imply in their contracts that two instances for which Equals returns true should be considered interchangeable; attaching supplemental information to instances without their knowledge may cause contractual expectations to be violated. – supercat May 1 '15 at 18:06
  • 1
    Why call TryGetValue in SetColor? ConditionalWeakTable.Remove doesn't throw an exception if the key doesn't exist. – OldFart May 1 '15 at 18:52
  • OldFart: you are right; I edited my answer to reflect this. – afeygin May 1 '15 at 19:32
  • Learn something new every day ... – Eric King May 1 '15 at 23:06
1

You don't.

The closest you can get to is Java style properties via extension methods, though if you actually need to store the color (as opposed to map it in the function) it can be difficult to do correctly since the extension class would hold references to the actual class if you do it the naive way.

The alternative is using standard inheritance or composition to extend the class, which you already mentioned.

1

Use a separate hash table to add properties to objects you don't control, and use your own access functions to wrap whatever behavior you want to appear to be uniform with respect to your added properties.

If garbage collection is likely to be a concern (your extra properties implemented this way will prevent the object from being garbage collected) this is perhaps the only good use of weak eq hash tables.

0

I don't see what's so "awful" about your last solution. You're given a definition of a Fruit, but you want it to be more descriptive. So you define an extension of the original definition with your Color addition.

Really, why do you prefer:

    Person.Fruits.FirstOrDefault().Color 

over:

    MyPerson.MyFruits.FirstOrDefault().Color

Only difference is the 'My' prefix which is purely cosmetic. You could also have a more descriptive name like ColoredFruit.

  • It's more than just cosmetic. Since this is a library, there can be many classes that I would need to inherit from in order to get this to work. It feels brittle, the work you have to do can easily snowball, and to me, the code smells. – MiniRagnarok May 1 '15 at 17:32
  • 1
    @MiniRagnarok: How do you figure? If MyFruit is a superset of Fruit, it should be nothing more than a subclass with additions. If you can't inherit Fruit without having to do other things, the library is broken. Badly. – Blrfl May 1 '15 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.