D-B*_*ett 4 c# delphi pascal converter
我正在尝试将Pascal类型转换为C#.我在谷歌上看了一下,但我没有设法找到答案,可能是因为我没有正确搜索,所以很抱歉,如果这是重复的话.
我有这两种Pascal类型:
type
TVector3i = array [0..2] of longint;
Tcolface = packed record
A, B, C: word;
SurfaceA, SurfaceB: word;
end;
Run Code Online (Sandbox Code Playgroud)
我知道
Tcolface = packed record
A, B, C: word;
SurfaceA, SurfaceB: word;
end;
Run Code Online (Sandbox Code Playgroud)
转换为:
struct Tcolface {
ushort A, B, C;
ushort SurfaceA, SurfaceB;
}
Run Code Online (Sandbox Code Playgroud)
但如何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;
Run Code Online (Sandbox Code Playgroud)
因此,为什么我要避免上课:D
如何
TVector3i = array [0..2] of longint;转换?
没有直接的等价物. TVector3i是静态数组的别名.C#没有类似的数组别名.您可以做的最好的事情是声明struct包含其中的int[]数组,并提供[] 索引器,以便与Pascal代码更紧密地语法兼容:
struct TVector3i
{
private int[] arr = new int[3];
public int this[int i]
{
get
{
return arr[i];
}
set
{
arr[i] = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新:根据您的示例,尝试这样的事情:
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>;
Run Code Online (Sandbox Code Playgroud)
将这个作为值类型可能是有充分理由的.这意味着赋值运算符是值副本而不是引用副本.结构可能是:
struct Vector3i
{
int X;
int Y;
int Z;
}
Run Code Online (Sandbox Code Playgroud)
您肯定会添加此类型所需的任何方法,以提供对您有用的操作.例如,[]操作员可以方便地进行索引访问.