0

I have a library (no source), to an certain object of which, I need to add some properties. What would be the a way to do it ? I'm aware I could extend the class and add properties to the child. As well I know there are NO Extension Properties in C# yet. What would you suggest ? Thank you !

The metadata of class could be something like :

 public class ResultClass
 {
    public IList<Item> Results { get; set; }
    public int TotalResults { get; set; }
 }

and I want to add a :

 String description;

to it. Thanks.

mishap
  • 8,176
  • 14
  • 61
  • 92

3 Answers3

2

There are a couple strategies you could take. Inheritance is the most obvious one. You might also consider composition. Can you give us some more details about what the object is and what properties you need to add to it and why?

After seeing the expanded question:

Either strategy outlined above (composition or inheritance) will probably work for you. Personally, I prefer composition. I think it better insulates you from changes that might be made to the third party library. It also forces you to work through the public interface of the library class, which is preferable when you have no knowledge or control of the internals of a class.

Here is the most basic example of composition.

public CompositeClass
{
    private ResultClass _resultClass = new ResultClass();

    public IList<Item> Results
    {
        get { return _resultClass.Results; }
        set { _resultClass.Results = value; }
    }

    public int TotalResults
    {
        get { return _resultClass.TotalResults; }
        set { _resultClass.TotalResults = value; }
    }

    //
    // New Property
    //
    public string Description { get; set; }
}
Nathanael
  • 1,782
  • 17
  • 25
  • I modified the question. Thank you! – mishap Sep 16 '11 at 23:12
  • +1 I agree: Either create a composite class or inherit from this and add your functionality. @Chuchelo: That's kind of the whole point to inheritence... to *extend* a class that already exists. – NotMe Sep 19 '11 at 16:42
  • @ChrisLively: I already got the extension approach, but what do you mean by composite class ? Thank you ! – mishap Sep 19 '11 at 19:16
  • 2
    I think it may be better to add a composition if integrating a new library into your code base. It allows an easier potential for swapping out the "library" at a later point in time and better encapsulates a third party tool into your app. The inheritance model may cause people to type it as the sub class making it harder to remove. – Dan Sep 19 '11 at 19:57
1

Why do you need to add properties? If this is for binding purposes then I would suggest creating a wrapper class or creating your own inherited type that can raise PropertyChanged events in response to various state changes in your third party types. Instead of telling us your proposed solution you should tell us the actual problem you are trying to solve. Also (as I can't vote to close/migrate), this is not really a valid discussion for this site.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • It's a reasonable question though. Migrating it to stackoverflow would be better than closing it. – Nathanael Sep 16 '11 at 22:16
  • @Nathaneal: Right, I guess I should have specified. I would chose to migrate if I had close authority here. – Ed S. Sep 16 '11 at 22:19
0

I think you are mixing up Extension Methods with Extension Properties. And the last ones do not exist in C#. So you should extend the class or create an inheriting class.

Community
  • 1
  • 1
Jacco
  • 3,251
  • 1
  • 19
  • 29