简体   繁体   中英

convert from enum to IEnumerable

Can you help me hww to corect this code

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace NTSoftHRM
{

    // ------------------------------------------------------------------------
    public class EnumValueList<T> : IEnumerable<T>
    {

        // ----------------------------------------------------------------------
        public EnumValueList()
        {
            IEnumerable<T> enumValues = GetEnumValues();
            foreach ( T enumValue in enumValues )
            {
                enumItems.Add( enumValue );
            }
        } // EnumValueList

        // ----------------------------------------------------------------------
        protected Type EnumType
        {
            get { return typeof( T ); }
        } // EnumType

        // ----------------------------------------------------------------------
        public IEnumerator<T> GetEnumerator()
        {
            return enumItems.GetEnumerator();
           // return ((IEnumerable<T>)enumItems).GetEnumerator();

        } // GetEnumerator

        // ----------------------------------------------------------------------
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        } // GetEnumerator

        // ----------------------------------------------------------------------
        // no Enum.GetValues() in Silverlight
        private IEnumerable<T> GetEnumValues()
        {
            List<T> enumValue = new List<T>();

            Type enumType = EnumType;

            return Enum.GetValues(enumType);

        } // GetEnumValues

        // ----------------------------------------------------------------------
        // members
        private readonly List<T> enumItems = new List<T>();

    } // class EnumValueList

} 

when bulid the error is: Cannot implicitly convert type 'System.Array' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) at return Enum.GetValues(enumType)

The issue is in your GetEnumValues method, Enum.GetValues returns an Array not an IEnumerable<T> . You need to cast it ie

Enum.GetValues(typeof(EnumType)).Cast<EnumType>();
    private IEnumerable<T> GetEnumValues()
    {
        Type enumType = EnumType;

        return Enum.GetValues(enumType).ToList<T>();
    } 

I assume this line is the one throwing the error?:

return Enum.GetValues(enumType);

According to the error message, you're missing a cast. Did you try adding a cast?:

return Enum.GetValues(enumType).Cast<T>();

The .GetValues() method on Enum returns an Array . And though it is something over which you can enumerate (it implements IEnumerable ), it is not a generic enumeration (it does not implement IEnumerable<T> at compile time, though the documentation indicates that it will be available at runtime.).

In order to return an IEnumerable as an IEnumerable<T> you need to cast it. Since collections aren't always directly covariant, there's a handy .Cast<T>() method provided to transform the collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM