views:

761

answers:

4

I have some extra functionality i need to add which includes adding a new property to an object and extending methods in another class that handles this object. I'm dealing with source code of a product (in C# 2.0) we use which i really don't want to modify, i just want to extend on it.

I'd ideally like to have a separate assembly to do this so that it's clear it's our code. It seems like partial classes and delegates may be the way to go, but I'm not sure if this is possible to do. Has anyone done something similar? or know any good articles?

A: 

Well you'll have to modify the original classes to also be "partial" classes and you'd have to make sure the namespace in the second assembly matches the one in the first and that both assemblies are references in the project that uses the assemblies. From there you are free to extend to your will.

gsnerf
Sure, you try that, and get back to me when it works, like in 20-30 years.
leppie
@leppie: why don't you provide an own answer instead of playing the smartass? @topic: It seems I was indeed wrong, partial classes from different assemblies are not treated as one class. Sorry for the wrong answer.
gsnerf
Yep, partials are a figment of the compiler's imagination. After compile time they go away, so you can't have partial classes span assemblies.
Will
A: 

Can you inherit from the class or is it sealed? Inheritance is the OO way to extend an existing class...

hfcs101
yes i can inherit, but it's all the code references to that class i dont want to have to change (for the sake of not touching existing code)...seems unavoidable
Chad
+2  A: 

Check out the Decorator pattern. This is exactly what you are looking for. You can inherit the exiting classes and extend it with any additional logic you have. There's also a Proxy pattern if you want hiding some base functionality.

Denis Vuyka
+2  A: 

Like hfcs101 said.

See if you can extend the class given some existing OO design patterns.

Take a look at:

See if you can find a pattern that closely matches your problem. Decorator looks like the best candidate.

Sardaukar
thanks for the other patterns, i'd never heard of these before, decorator is spot on as you and @Denis_Vuyka mentioned
Chad