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.

1
  • Too bad this isn't marked as a partial class. Commented Oct 30, 2011 at 20:49

3 Answers 3

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; } } 
Sign up to request clarification or add additional context in comments.

4 Comments

I modified the question. Thank you!
+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.
@ChrisLively: I already got the extension approach, but what do you mean by composite class ? Thank you !
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.
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.

2 Comments

It's a reasonable question though. Migrating it to stackoverflow would be better than closing it.
@Nathaneal: Right, I guess I should have specified. I would chose to migrate if I had close authority here.
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.

2 Comments

Thanks! I actually meant there are NO extension properties.
When you say "extend", do you mean using extension methods ? I need to add properties. So only way is inheritance ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.