'Disable implicit conversion of third party classes in C# without writing wrappers?
I use an engine which got Vector3 and Vector2 classes.
However there are regular issues by accidentally passing a Vector2 to a method which needs a Vector3.
Currently it's automatically converted to a Vector3 of (x, y, 0). After I experience runtime issues it always takes some time to I find these trivial causes...
Is it possible to somehow block the implicit conversion between these two third party classes so I can detect these problems at compile time?
Currently my only idea is to write wrappers and replace every usage of Vector3 and Vector2 with them.
But it seems tedious. Is there any other way?
Solution 1:[1]
I suppose that Vector3 class has this code:
public static implicit operator Vector3(Vector2 vector) => new Vector3(vector.x, vector.y, 0);
Unfortunately, you cannot block the conversion (C# built-in feature). Honestly, it is a poor design choice made by the Engine designers...
You can only create your own wrappers to overcome this issue.
public class Vector2Ex {
public Vector2 vector2 { get; set; }
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | NazaRN |
