-1

I am using C# .Net 4.7.2

I want to analyse a type by using reflection. One part of the analysing is to identify the properties which are of any kind of collection. Such as Collection, List, Array, IEnumerable, etc.

I thought it would be easy and searched for types implementing IEnumerable. Well, seems to be not that easy. String for example implements IEnumrable as well, but is not a collection type.

Is there anything more, that I could use to assert, that the property is some kind of collection?

Clarification Collection type means any type that can store or manage multiple members (the exact number of memberitems doesn't matter). One may also call it a container type. E.g. Collection, List, Array, Dictionary, ...

IEnumarable however isn't a good criteria to seperate collection types from non collection types. String for example implements IEnumerable because it provides an array of char. But String isn't a collection type.

My concern is, treating types like String the wrong way - as a collection. In case of string I would end up reading the array of char, but I really wanted to read the string itself.

In case of a real collection type, e.g. List<int> I do want to read every member. In this example every integer value.

There is no need to modify the values!

GeorgeDuke
  • 151
  • 15
  • 3
    So what is your definition of a collection type? – Patrick Hofman Nov 28 '18 at 10:32
  • 1
    A string _is_ a collection type, it holds a collection of `char`s. You need a stricter definition of "collection", or a blacklist of (element) types you want to ignore. But is a `char[]` a collection? – CodeCaster Nov 28 '18 at 10:35
  • I would describe a collection type as a type, that can manage multiple members. Providing functions like *add* and *delete*. Mmh, may that be the criteria? – GeorgeDuke Nov 28 '18 at 10:37
  • 1
    So you can't add items to an array. Is an array a collection type? – Patrick Hofman Nov 28 '18 at 10:37
  • @CodeCaster char[] is an array, so yes, it is a collection. But if one is searching for a string, one is usually not looking for an array of char – GeorgeDuke Nov 28 '18 at 10:39
  • You can't add or delete anything to or from an array as well. I'm not arguing what people use strings for and why they'd prefer a string over a char array, I'm merely stating the fact that a string _is_ a char array. If you want modifyable collections, then search for the interface `ICollection`. – CodeCaster Nov 28 '18 at 10:40
  • @Patrick Hofman. Please don't do any kind of quiz with me. As I mentioned in the original question, yes array is a collection type – GeorgeDuke Nov 28 '18 at 10:40
  • We are not doing a quiz with you. We are helping you set your requirements, which are very unclear from your question. At least, if you want to keep this question open and get an answer... – Patrick Hofman Nov 28 '18 at 10:41
  • 1
    We're not quizzing you or trying to annoy you, we're trying to evoke a response that makes this an answerable question. – CodeCaster Nov 28 '18 at 10:41
  • 2
    The nut of the problem here, OP, is that the requirement is not clearly defined, and therefore the correctness of the code is ambiguous. If you can't answer Patrick & everyone else in unambiguous English, you will have trouble writing the correct code. This sort of inquiry may seem pedantic but it is absolutely a critical part of programming. – John Wu Nov 28 '18 at 10:42
  • The solution to the problem is found. But did the clarification in my original question help to understand the problem? Please don't get me wrong, I appreciate your help! – GeorgeDuke Nov 28 '18 at 16:23

1 Answers1

1

You have to check the IList and IDictonary instead of IEnumerable here the sample, from my framework:

    public static bool IsContainerType( Type type )
    {
        TypeInfo typeInfo = type.GetTypeInfo();

        if ( typeInfo.IsArray == true )
            return true;

        if ( typeof( System.Collections.IList ).IsAssignableFrom( type ) == true )
            return true;

        if ( typeof( System.Collections.IDictionary ).IsAssignableFrom( type ) == true )
            return true;

        return false;
    }
György Gulyás
  • 1,290
  • 11
  • 37