繁体   English   中英

将Pascal'type'转换为C#

[英]Converting Pascal 'type' to C#

我正在尝试将Pascal类型转换为C#。 我在谷歌上看了一下,但我没有设法找到答案,可能是因为我没有正确搜索,所以很抱歉,如果这是重复的话。

我有这两种Pascal类型:

type
  TVector3i = array [0..2] of longint;

  Tcolface = packed record
    A, B, C: word;
    SurfaceA, SurfaceB: word;
  end;

我知道

Tcolface = packed record
  A, B, C: word;
  SurfaceA, SurfaceB: word;
end;

转换为:

struct Tcolface {
  ushort A, B, C;
  ushort SurfaceA, SurfaceB;
}

TVector3i = array [0..2] of longint;如何TVector3i = array [0..2] of longint; 兑换?

我试图避免使用/编写一个类,因为当我转换其余的Pascal代码时,它将期望该类型作为数组,并且我试图避免将其转换为.x .y和.z。

我确实考虑过做float[] variablename = new float[3]; ,但是一旦我得到List<float[]> variblename就会变得有点复杂。

完整的代码是:

TVector3i = array [0..2] of Longint;
TVector3f = array [0..2] of Single;
TVector3d = array [0..2] of Double;

TVector4i = array [0..3] of Longint;
TVector4f = array [0..3] of Single;
TVector4d = array [0..3] of Double;

TMatrix3i = array [0..2] of TVector3i;
TMatrix3f = array [0..2] of TVector3f;
TMatrix3d = array [0..2] of TVector3d;

TMatrix4i = array [0..3] of TVector4i;
TMatrix4f = array [0..3] of TVector4f;
TMatrix4d = array [0..3] of TVector4d;

因此,为什么我要避免上课:D

TVector3i = array [0..2] of longint;如何TVector3i = array [0..2] of longint; 兑换?

没有直接的等价物。 TVector3i是静态数组的别名。 C#没有类似的数组别名。 你可以做的最好的事情是声明一个包含int[]数组的struct ,并提供一个[] 索引器,以便与Pascal代码更紧密地语法兼容:

struct TVector3i
{
    private int[] arr = new int[3];

    public int this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

更新 :根据您的示例,尝试这样的事情:

struct TVector3<T>
{
    private T[] arr = new T[3];

    public T this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

struct TVector4<T>
{
    private T[] arr = new T[4];

    public T this[int i]
    {
        get
        {
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

using TVector3i = TVector3<int>;
using TVector3f = TVector3<float>;
using TVector3d = TVector3<double>;

using TVector4i = TVector4<int>;
using TVector4f = TVector4<float>;
using TVector4d = TVector4<double>;

using TMatrix3i = TVector3<TVector3i>;
using TMatrix3f = TVector3<TVector3f>;
using TMatrix3d = TVector3<TVector3d>;

using TMatrix4i = TVector4<TVector4i>;
using TMatrix4f = TVector4<TVector4f>;
using TMatrix4d = TVector4<TVector4d>;

将这个作为值类型可能是有充分理由的。 这意味着赋值运算符是值副本而不是引用副本。 结构可能是:

struct Vector3i
{
    int X;
    int Y;
    int Z;
}

您肯定会添加此类型所需的任何方法,以提供对您有用的操作。 例如, []运算符使索引访问变得方便。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM