Can I add an implicit conversion for two classes which I don't directly control?

后端 未结 2 1288
粉色の甜心
粉色の甜心 2020-12-17 14:56

I\'d like to be able to implicitly convert between two classes which are otherwise incompatible.

One of the classes is Microsoft.Xna.Framework.Vector3,

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-17 15:48

    No, you can't. The implicit operator has to be defined as a member of one of the classes. However, you can define an extension method (your example didn't work as extension methods have to be in a public static class).

    public static class ConverterExtensions
    {
        public static Vector ToVector (this Vector3 input)
        {
          //convert
        }
    }
    

提交回复
热议问题