Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do Partial Classes Extend Existing Code 2

Status
Not open for further replies.

PGO01

Programmer
Jan 8, 2008
156
GB
I get the impression that a partial class can extend existing (compiled) code. Is this correct?

I've tried to mock up a simple example but I can't do it.

Here's what I did:

1. I created a simple partial class and compiled it.
2. I created a new console app. and added a reference to my previously created assembly.
3. I created a partial class in my console app. with the same name and same namespace as the previously compiled one.
4. I added a new property.

 
My understanding of partial classes is that they work within the assembly. In other words, it does not work with compiled code.

The partial class was created for the main purpose of designer code like webforms and winforms. the auto-generated code goes into one file, and your code goes into another file. At compile time it's the same object. But design time code is separated to ease readability and maintainability.

Design patterns liek Proxy, Decorator, Adapter are the common ways to extend functionality.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
You can use partial classes like that, but the class you're trying to extend has to already be marked as being a partial.

Extension methods (new to 3.x) are probably what you're looking for.

Chip H.

____________________________________________________________________
www.chipholland.com
 
thanks for the correction chiph

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks both for your answers.

ChipH I've tried compiling my own partial class and extending it in another assembly, but once compiled I don't think it's recognised as being partial any more...

Extension methods look very interesting though...
 
You can also apply some simple programming patterns like the Decorator or Proxy pattern.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top