Linked Questions

Popular Questions

Add items to any object of ICollection<T> using reflection

Asked by At

I'm trying to support mapping to/from any kind of collection that implements ICollection<T> via reflection, because ICollection<T> requires implementation of the Add method.

This works fine for most common collection types, but fails for edge cases like LinkedList<T> where the Add method is hidden and can only be called by casting the LinkedList<T> to ICollection<T>.

However it's not possible to convert to ICollection<> because it is not covariant.

The other option I was considering was searching for both implicit and explicit implementations of Add, but I don't see any information on how to do this when the interface is generic?

What would be the correct approach to take?

Updated to show code snippet where I'm reflecting from xml to object mapping.

    private object CollectionXmlNodeListToObject(
         XmlNodeList nodeList, System.Type collectionType)
        // this is not possible because ICollection<> is not covariant
        object collection = Convert.ChangeType(
              CreateInstanceOfType(collectionType), ICollection<>);
        Type containedType = collectionType.GetTypeInfo().GenericTypeArguments[0];

        foreach (XmlNode node in nodeList)
            object value = CreateInstanceOfType(containedType);

            if (containedType.IsClass && MetaDataCache.Contains(containedType))
                value = ToObject(value, node, node.Name);
                value = node.InnerText;

            // this throws NullReferenceException when the type is LinkedList,
            // because this is explicitly implemented in LinkedList
                 .Invoke(collection, new[] { value });

        return collection;

I am writing a small framework to map from object to xml using class and property attributes. So I cannot use generics because all of this is being done at runtime.

I initially was checking for IEnumerable before, but ran into other oddities with it (strings implement IEnumberable and are immutable) that I decided it was safest to stick to ICollection<>

Related Questions