MojoFilter
MojoFilter

Reputation: 12276

Extend No-Constructor class

In C#, is it possible to extend a class that has no constructors?

Maybe I'm thinking about this incorrectly and just need a kick in the crotch. I have a Silverlight class that extends System.Windows.Media.Transform, With the official release of Silverlight 2, Transform now has no constructor. So, when I compile my class, I get an error saying that 'The type '...Transform' has no constructors defined.'

Is it still possible to extend this class in a useful way? If not, I'm going to be drawing an awful lot of sad faces.

Upvotes: 4

Views: 5145

Answers (4)

Partha
Partha

Reputation:

As long as its not sealed, you should be able to extend it.

Upvotes: 0

pero
pero

Reputation: 4259

It is impossible that some class has no constructor. Every class has at least one AFAIK. If you don't write it C# compiler will insert an default (parameterless) constructor for you.

Upvotes: 0

caskey
caskey

Reputation: 12695

Hiding all public constructors is a technique used to prevent subclassing and force developers to use the class as intended. It may be the implementor wants you to use an Adapter or Facade or even a Proxy at the time of extension.

There may be important lifecycle details in the constructor that require the use as the original implementor intended, or it may be an oversight by the developer.

I've seen attempts at conversion to the Factory pattern where this is done in a manner that prevents subclassing. I.e. there is no protected constructor made available to subclasses. Sometimes advanced object patterns can strain the in-built capabilities of a language.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500385

The sole constructor to Transform is internal, so you can't derive from it yourself.

Upvotes: 9

Related Questions