dimanche 30 août 2015

c# Cast int[] into MyType[] where MyType inherited from Enum?

I try to cast an int array int[] into Rights array Rights[], Rights is Enum by example... But it can be other type than rights but always inherited from Enum. I got the final type in a Type variable. I can create the Enum[] array but not the final Rights[] array for my methodInfo.Invoke(obj,param.ToArray()).

List<object> param = new List<object>();
ParameterInfo pi = ...
if (pi.ParameterType.IsArray && pi.ParameterType.GetElementType().IsEnum)
{
    List<Enum> enums = new List<Enum>();
    foreach (int i in GetTabInt())
    {
        enums.Add((Enum)Enum.ToObject(pi.ParameterType.GetElementType(), i));
    }
    param.Add(enums.ToArray()); // got Enum[] not Rights[]
}

Thank you for helping me!





Aucun commentaire:

Enregistrer un commentaire