Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping C# classes to use with polymorphism through a common interface

I've got several C# classes each with similar properties. (They're part of an SDK and their code can’t be changed.)

  • Person.Name
  • Product.Name
  • Order.Name

I want to use these classes polymorphically, but they don’t implement a common interface or derive from a common base class, so that’s not possible. To get around this, I’d like to wrap each one in another class that does implement a common interface, and wire-up each class property to its corresponding interface property.

What would be a suitable name for the wrapper classes? Wrapper, Decorator, Adaptor, Proxy? Does this pattern have a name? Is there a better approach?

(I don't want to use dynamic duck-typing or an impromptu interface.)

like image 222
Chris Fulstow Avatar asked Jun 08 '26 16:06

Chris Fulstow


1 Answers

It looks like Adapter, because you are adapting the existing interfaces to the specific requirements.

like image 102
Ryszard Dżegan Avatar answered Jun 11 '26 05:06

Ryszard Dżegan