I have a third-party library (Mogre), in which is a struct (Vector3). I would like to add an overload for the ‘+’ operator (no override needed) for this type but am not sure how.
I cannot use extension methods as it is an operator I want to extend; the class is not sealed but not partial either, so if I try and define it again with my new operator overload I get conflicts.
Is it possible to extend a type like this? What is the best way to do it?
You cannot add an operator overload to a third-party type — really any class you don’t have the ability to edit. Operator overloads must be defined inside their type they are to operate on (at least one of the args). Since it’s not your type, you can’t edit it, and
structs cannot be extended.But, even if it was a non-
sealed class, you’d have to sub-class, which would ruin the point because they you’d have to use the subclass and not the superclass with the operator, since you can’t define the operator overload between the base types…You could do the overload completely between instances of the subclass, but then you’d have to use your new subclass wherever you wanted to do the overload instead of the original superclass, which would look clunky and probably not what you want: