- Home /
Question by
Tyrathect · Feb 03, 2016 at 03:33 PM ·
c#monoextension-methodsambiguous reference
Ambiguous reference issue on extension methods
You can't define the same extension method for more than one Vector class.
When you have and extension method for Vector3 and one with the same name for Vector2, you cannot call the Vector3 version because the compiler can't resolve the reference. Even if you add an explicit cast, it still doesn't work.
Obviously the issue is the implicit cast operator from Vector3 to Vector2, but in this case it causes serious problems.
Is there a way around this? Maybe an attribute like [DontAllowImplicitCasts] or something?
example:
public static class ExtensionMethods
{
public static bool AlmostZero(this Vector3 value)
{
return value.sqrMagnitude < 0.00001f;
}
public static bool AlmostZero(this Vector2 value)
{
return value.sqrMagnitude < 0.00001f;
}
}
void SomeOtherMethodInSomeOtherClass()
{
Vector3 vector = _rigidBody.Velocity;
//***********************************
//*** this call can't be resolved ***
//***********************************
if (vector.AlmostZero())
DoSomethingWhenStopped();
else
DoSomethingWhenMoving();
//***********************************
//*** this also can't be resolved ***
//***********************************
if ((Vector3)vector).AlmostZero())
DoSomethingWhenStopped();
else
DoSomethingWhenMoving();
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity compilation order 2 Answers
Connection issues with C# and MySql using Mono. 0 Answers
What's the best way to hide classes when sharing C#? 2 Answers