在 C# 中,是否可以使用非字符串或 int 类型的自定义索引器 属性?
In C# is it possible to have an custom Indexer property of a type that is NOT a string or an int?
在 C# 中,是否可以使用非字符串或整数类型的索引器 属性?
例如,我有一个自定义对象,它是一个二维矢量坐标图。以我的地图 class 为基础...
public class TileMap
{
/// <summary>
/// The holds an array of tiles
/// </summary>
MapTile[,] _map;
/// <summary>
/// Gets the <see cref="PathFindingMapTile"/> with the specified position.
/// </summary>
/// <value>
/// The <see cref="PathFindingMapTile"/>.
/// </value>
/// <param name="position">The position.</param>
/// <returns></returns>
public MapTile this[Vector2 position] // Indexer declaration
{
get
{
int x = (int)position.x;
int y = (int)position.y;
return _map[x, y];
}
}
/// <summary>
/// Initializes a new instance of the <see cref="TileMap"/> class.
/// </summary>
/// <param name="length">The length.</param>
/// <param name="height">The height.</param>
public TileMap(int length, int height)
{
_map = new MapTile[length, height];
}
}
编译没有问题。但是调用代码失败并出现两个错误
基地class
internal abstract class MyBase
{
/// <summary>
/// Gets (or privately sets) the tile map
/// </summary>
protected PathFindingMapTile[,] TileMap { get; private set; }
}
派生class
internal class MyDerived : MyBase
{
public void MyMethod()
{
Vector2 possiblePosition;
MapTile possibleTile = null;
possibleTile = this.TileMap[possiblePosition]; // <-- This line wont compile
}
}
编译错误:
Cannot implicitly convert type 'UnityEngine.Vector2' to 'int'
Wrong number of indices inside []; expected 2
为什么有两个索引?我只说了一个,立场。有什么建议吗?
更新 - 关注 Rufus 评论。更正了基础 class.
的 "Tilemap" 属性 的 return 类型
internal abstract class MyBase
{
/// <summary>
/// Gets (or privately sets) the tile map
/// </summary>
protected TileMap TileMap { get; private set; }
}
问题是你的 TileMap
属性 不是 TileMap
类型,而是 PathFindingMapTile[,]
类型,需要两个索引。
在 C# 中,是否可以使用非字符串或整数类型的索引器 属性?
例如,我有一个自定义对象,它是一个二维矢量坐标图。以我的地图 class 为基础...
public class TileMap
{
/// <summary>
/// The holds an array of tiles
/// </summary>
MapTile[,] _map;
/// <summary>
/// Gets the <see cref="PathFindingMapTile"/> with the specified position.
/// </summary>
/// <value>
/// The <see cref="PathFindingMapTile"/>.
/// </value>
/// <param name="position">The position.</param>
/// <returns></returns>
public MapTile this[Vector2 position] // Indexer declaration
{
get
{
int x = (int)position.x;
int y = (int)position.y;
return _map[x, y];
}
}
/// <summary>
/// Initializes a new instance of the <see cref="TileMap"/> class.
/// </summary>
/// <param name="length">The length.</param>
/// <param name="height">The height.</param>
public TileMap(int length, int height)
{
_map = new MapTile[length, height];
}
}
编译没有问题。但是调用代码失败并出现两个错误
基地class
internal abstract class MyBase
{
/// <summary>
/// Gets (or privately sets) the tile map
/// </summary>
protected PathFindingMapTile[,] TileMap { get; private set; }
}
派生class
internal class MyDerived : MyBase
{
public void MyMethod()
{
Vector2 possiblePosition;
MapTile possibleTile = null;
possibleTile = this.TileMap[possiblePosition]; // <-- This line wont compile
}
}
编译错误:
Cannot implicitly convert type 'UnityEngine.Vector2' to 'int'
Wrong number of indices inside []; expected 2
为什么有两个索引?我只说了一个,立场。有什么建议吗?
更新 - 关注 Rufus 评论。更正了基础 class.
的 "Tilemap" 属性 的 return 类型internal abstract class MyBase
{
/// <summary>
/// Gets (or privately sets) the tile map
/// </summary>
protected TileMap TileMap { get; private set; }
}
问题是你的 TileMap
属性 不是 TileMap
类型,而是 PathFindingMapTile[,]
类型,需要两个索引。