铸造类型数组通用阵列? [英] Casting Type array to Generic array?
问题描述
问题的短版 - 为什么我不能这样做呢?我仅限于.NET 3.5。
T [] genericArray;//很明显,T应该浮动!
genericArray =新的T [3] {1.0F,2.0F,0.0};//不能做到这一点无论,凭啥不
genericArray =新的浮动[3] {1.0F,2.0F,0.0};
加长版 -
我的Unity引擎在这里工作,虽然这并不重要。什么是 - 我想它固定Vector2(2花车)的Vector3和(3花车)和我的通用矢量℃之间扔转换;>类。我能不能直接转换类型的通用阵列。
使用UnityEngine;公共结构矢量< T>
{
私人只读T [] _axes; #区域构造
公共向量(INT axisCount)
{
this._axes =新的T [axisCount]
} 公共向量(T X,T Y)
{
this._axes =新的T [2] {X,Y};
} 公共向量(T X,Tÿ,T Z)
{
this._axes =新的T [3] {X,Y,Z};
} 公共向量(Vector2 vector2)
{
//这不起作用
this._axes =新的T [2] {vector2.x,vector2.y};
} 公共向量(Vector3类型的Vector3)
{
//也做到这一点
this._axes =新的T [3] {vector3.x,vector3.y,vector3.z};
}
#endregion #区域属性
公共ŧ这个[INT I]
{
{返回_axes [I] }
集合{_axes [I] =价值; }
} 公众的T x
{
{返回_axes [0];}
集合{_axes [0] =价值; }
} 公共ŧÿ
{
{返回_axes [1]; }
集合{_axes [1] =值; }
} 公共ŧž
{
得到
{
返回this._axes.Length< 2?默认(T):_axes [2];
}
组
{
如果(this._axes.Length 2)
返回; _axes [2] =价值;
}
}
#endregion #区域类型转换器
公共静态明确经营者矢量< T>(Vector2 vector2)
{
矢量< T>矢量=新的矢量< T>(vector2); 返回向量;
} 公共静态明确经营者矢量< T>(Vector3类型的Vector3)
{
矢量< T>矢量=新的矢量< T>(的Vector3); 返回向量;
}
#endregion
}
常规手段的作品是任何的类型。
您例如code是不通用的,因为它只能当且仅当 T
是浮动
虽然你不能一个的Vector2D转换为矢量< T&放大器; GT,你可以,当然,转换的Vector2D为矢量<浮取代。一个转换
方法添加到的Vector2D或提供一套扩展的方法是这样的:
公共静态类VectorExtensions
{
公共静态矢量<浮球GT; ToGenericVector(此的Vector2D向量)
{
返回新的矢量<浮球GT;(vector.X,vector.Y);
} 公共静态的Vector2D ToVector2D(这个矢量<浮球GT;向量)
{
返回新的Vector2D(vector.X,vector.Y);
}
}
用法:
矢量<浮球GT; V =新的矢量<浮球GT;(3,5);的Vector2D V2 = v.ToVector2D();
The short version of the question - why can't I do this? I'm restricted to .NET 3.5.
T[] genericArray;
// Obviously T should be float!
genericArray = new T[3]{ 1.0f, 2.0f, 0.0f };
// Can't do this either, why the hell not
genericArray = new float[3]{ 1.0f, 2.0f, 0.0f };
Longer version -
I'm working with the Unity engine here, although that's not important. What is - I'm trying to throw conversion between its fixed Vector2 (2 floats) and Vector3 (3 floats) and my generic Vector<> class. I can't cast types directly to a generic array.
using UnityEngine;
public struct Vector<T>
{
private readonly T[] _axes;
#region Constructors
public Vector(int axisCount)
{
this._axes = new T[axisCount];
}
public Vector(T x, T y)
{
this._axes = new T[2] { x, y };
}
public Vector(T x, T y, T z)
{
this._axes = new T[3]{x, y, z};
}
public Vector(Vector2 vector2)
{
// This doesn't work
this._axes = new T[2] { vector2.x, vector2.y };
}
public Vector(Vector3 vector3)
{
// Nor does this
this._axes = new T[3] { vector3.x, vector3.y, vector3.z };
}
#endregion
#region Properties
public T this[int i]
{
get { return _axes[i]; }
set { _axes[i] = value; }
}
public T X
{
get { return _axes[0];}
set { _axes[0] = value; }
}
public T Y
{
get { return _axes[1]; }
set { _axes[1] = value; }
}
public T Z
{
get
{
return this._axes.Length < 2 ? default(T) : _axes[2];
}
set
{
if (this._axes.Length < 2)
return;
_axes[2] = value;
}
}
#endregion
#region Type Converters
public static explicit operator Vector<T>(Vector2 vector2)
{
Vector<T> vector = new Vector<T>(vector2);
return vector;
}
public static explicit operator Vector<T>(Vector3 vector3)
{
Vector<T> vector = new Vector<T>(vector3);
return vector;
}
#endregion
}
"Generic" means "works with any type".
Your example code is not generic, because it only works if and only if T
is float
.
While you can't convert a Vector2D to a Vector<T>, you can, of course, convert a Vector2D to a Vector<float>. Add a Convert
method to Vector2D or provide a set of extension methods like this:
public static class VectorExtensions
{
public static Vector<float> ToGenericVector(this Vector2D vector)
{
return new Vector<float>(vector.X, vector.Y);
}
public static Vector2D ToVector2D(this Vector<float> vector)
{
return new Vector2D(vector.X, vector.Y);
}
}
Usage:
Vector<float> v = new Vector<float>(3, 5);
Vector2D v2 = v.ToVector2D();
这篇关于铸造类型数组通用阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!