我可以为我不直接控制的两个类添加隐式转换吗? [英] Can I add an implicit conversion for two classes which I don't directly control?

查看:27
本文介绍了我可以为我不直接控制的两个类添加隐式转换吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在两个不兼容的类之间进行隐式转换.

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

其中一个类是 Microsoft.Xna.Framework.Vector3,另一个只是 F# 项目中使用的 Vector 类.我正在用 XNA 用 C# 编写一个 3d 游戏,虽然它是用 3D 绘制的,但游戏玩法只发生在二维(这是一个鸟瞰图).F# 类使用 2D 向量处理物理:

One of the classes is Microsoft.Xna.Framework.Vector3, and the other is just a Vector class used in an F# project. I'm writing a 3d game in C# with XNA, and -- although it's drawn in 3D, the gameplay takes place in only two dimensions (it's a birds-eye-view). The F# class takes care of the physics, using a 2D vector:

type Vector<'t when 't :> SuperUnit<'t>> =
    | Cartesian of 't * 't
    | Polar of 't * float
    member this.magnitude =
        match this with
        | Cartesian(x, y) -> x.newValue(sqrt (x.units ** 2.0 + y.units ** 2.0))
        | Polar(m, _) -> m.newValue(m.units)
    member this.direction =
        match this with
        | Cartesian(x, y) -> tan(y.units / x.units)
        | Polar(_, d) -> d
    member this.x =
        match this with
        | Cartesian(x, _) -> x
        | Polar(m, d) -> m.newValue(m.units * cos(d))
    member this.y =
        match this with
        | Cartesian(_, y) -> y
        | Polar(m, d) -> m.newValue(m.units * sin(d))

这个向量类使用物理项目使用的单位系统,它采用原生 F# 测量单位并将它们组合在一起(距离、时间、质量等单位).

This vector class makes use of the unit system used by the physics project, which takes native F# units of measure and groups them together (units of Distance, Time, Mass, etc).

但 XNA 使用它自己的 Vector3 类.我想添加一个从 F# Vector 到 XNA Vector3 的隐式转换,它负责处理游戏玩法发生在哪两个维度,其中轴是向上"等.这很简单,只需 Vector v ->new Vector3(v.x, v.y, 0) 什么的.

But XNA uses its own Vector3 class. I want to add an implicit conversion from the F# Vector to the XNA Vector3 which takes care of which two dimensions the gameplay takes place in, which axis is "up", etc. It'd be simple, just Vector v -> new Vector3(v.x, v.y, 0) or something.

我不知道该怎么做.我无法在 F# 中添加隐式转换,因为类型系统(正确地)不允许它.我无法将它添加到 Vector3 类,因为它是 XNA 库的一部分.据我所知,我不能使用扩展方法:

I can't figure out how to do it though. I can't add an implicit conversion in F# because the type system (rightly) doesn't allow it. I can't add it to the Vector3 class because that is part of the XNA library. As far as I can tell I can't use an extension method:

class CsToFs
{
    public static implicit operator Vector3(this Vector<Distance> v)
    {
        //...
    }
}

this 关键字有误,并且

class CsToFs
{
    public static implicit operator Vector3(Vector<Distance> v)
    {
        return new Vector3((float)v.x.units, (float)v.y.units, 0);
    }

    public static void test()
    {
        var v = Vector<Distance>.NewCartesian(Distance.Meters(0), Distance.Meters(0));
        Vector3 a;
        a = v;
    }
}

a = v; 上的错误(不能隐式转换...).

is an error on a = v; (cannot implicitly convert...).

有没有办法做到这一点而不能将演员表放在任何一个类中?作为最后的手段,我可​​以打开 Microsoft.Xna.Framework 并在 F# 中进行转换,但这对我来说似乎是错误的——物理库不应该知道或关心我使用的框架编写游戏.

Is there a way to do this without being able to put the cast in either of the classes? As a last resort I could open Microsoft.Xna.Framework and do the conversion in F#, but that seems wrong to me -- the physics library shouldn't know or care what framework I'm using to write the game.

推荐答案

不,你不能.隐式运算符必须定义为其中一个类的成员.但是,您可以定义扩展方法(您的示例不起作用,因为扩展方法必须在 public static class 中).

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
    }
}

这篇关于我可以为我不直接控制的两个类添加隐式转换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆