lundi 7 août 2017

Creating a generic funtion for adding items to a HashSet

in my C# program, I want to add items to a HashSet<T> via reflection. With a List<T> this is not a problem, because I can cast to the parameterless IList interface:

foreach (PropertyInfo property in myClass.GetType().GetProperties())
{
  object value = property.GetValue(myClass);
  IList valueAsIList = value as IList;
  if (valueAsIList != null)
    valueAsIList.Add(item2Insert);
}

Now I want to do the same thing with HashSet<T>, but there is no such thing like IList where I could cast it to and call the Add method. Is there any other way to do it?

Thanks in advance,
Frank





Aucun commentaire:

Enregistrer un commentaire