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 i.e.
Enum.GetValues(typeof(EnumType)).Cast<EnumType>();
private IEnumerable<T> GetEnumValues()
{
Type enumType = EnumType;
return Enum.GetValues(enumType).ToList<T>();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With