将所有IEnumerable <string>更改为枚举类型List

时间:2019-07-19 19:42:38

标签: c# generics types enums ienumerable

概念很简单...获取所有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();
}

愿景是:

  1. 从IEnumerable获取列表/名称数组。
  2. 将此列表或每个元素转换为必需的类型。
  3. 获取IEnumerable或类似的内容。
  4. 在主要功能中使用此列表。

请注意,我尝试

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>;
    }

谢谢!

0 个答案:

没有答案
相关问题