概念很简单...获取所有Enum.Getnames(typeof(T),string),并将此字符串数组转换为List并轻松从列表中获取枚举名称。
然后,我想将解析为泛型类型的值添加到新的临时列表中,出现以下错误:
CS1503:参数“ 1”:无法从“对象”转换为“ T”。
public enum PieceTypes
{
Left_Top,
Top_Right,
Right_Bottom,
Bottom_Left,
Left_Right,
Top_Bottom,
Empty
};
“主要”功能:
private void RotatePieceCurly()
{
GetEnumIndexs<PieceTypes>();
//This List need, with enum values
var ElementOfEnums = new List<PieceTypes>();
foreach (var index in ElementOfEnums)
{
/* when index is equal object Piece type then want get next array index
* Example :
* this fragment rotate according to clokwise notation
* when I have fragment started from left edge and end at top corner, after rotate
* get next List index so start from Top and end end at right corner */
if (index == this.PieceType)
{
//get index of currenty piece
int indexNumber = ElementOfEnums.FindIndex((match) => match == index);
// if index is last then i get first change to first index there is only 4 posible rotate format
if (indexNumber >= 3)
{
this.PieceType = ElementOfEnums[0];
return;
}
else
//else get next fragment
//there is problem i need List with all enum names to get next
this.PieceType = ElementOfEnums[indexNumber + 1];
return;
}
}
}
存在问题
public IEnumerable<T> GetEnumIndexs<T>() where T : struct, Enum, IConvertible
{
var EnumList = new List<T>();
foreach (var index in Enum.GetNames(typeof(T)))
{
var type = Enum.Parse(typeof(T), index);
//there is problem \/ cannot add this element
EnumList.Add(type);
// EnumList.Add();
}
var nextEnumTest = tempList[0] as T;
return EnumList.GetEnumerator as IEnumerable();
}
愿景是:
请注意,我尝试
Enumerable.Cast(IEnumerable)
https://docs.microsoft.com/pl-pl/dotnet/api/system.linq.enumerable.cast?view=netframework-4.8
这是不可能的。
如果有人对此有更好的概念,请看看。 编辑1: 编辑可能的解决方案(概述),但不起作用
public IEnumerable<T> GetEnumIndexs<T>() where T : struct, Enum, IConvertible
{
var example = Enum.GetNames(typeof(T));
var exampleListToReturn = new List<T>();
foreach (var index in example)
{
ex.Add(( /* T */)index);
}
return exampleListToReturn.GetEnumerator();
}
编辑2:
public IEnumerable<T> GetEnumIndexs<T>() where T : struct, Enum, IConvertible
{
var example = Enum.GetNames(typeof(T));
var exampleList = new List<T>();
foreach (var index in example)
{
var typeNew = Enum.Parse(typeof(T), index);
exampleList.Add((T)typeNew);
}
return exampleList as IEnumerable<T>;
}
谢谢!