<p>example:</p>

<pre><code>public static void DoSomething&lt;K,V&gt;(IDictionary&lt;K,V&gt; items) {
   items.Keys.Each(key =&gt; {
      if (items[key] **is IEnumerable&lt;?&gt;**) { /* do something */ }
      else { /* do something else */ }
}
</code></pre>

<p>Can this be done without using reflection? How do I say IEnumerable in C#? Should I just use IEnumerable since IEnumerable&lt;> implements IEnumerable? </p>

[{'answer_id': 74648, 'author': 'Jorge Ferreira', 'author_id': 6508, 'author_profile': 'https://Stackoverflow.com/users/6508', 'pm_score': 2, 'selected': False, 'text': '<pre><code>if (typeof(IEnumerable).IsAssignableFrom(typeof(V))) {\n</code></pre>\n'}, {'answer_id': 74772, 'author': 'Isak Savo', 'author_id': 8521, 'author_profile': 'https://Stackoverflow.com/users/8521', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m not sure I understand what you mean here. Do you want to know if the object is of <strong>any generic type</strong> or do you want to test if it is a <strong>specific generic type</strong>? Or do you just want to know if is enumerable?</p>\n\n<p>I don\'t think the first is possible. The second is definitely possible, just treat it as any other type. For the third, just test it against IEnumerable as you suggested.</p>\n\n<p>Also, you cannot use the \'is\' operator on types. </p>\n\n<pre><code>// Not allowed\nif (string is Object)\n  Foo();\n// You have to use \nif (typeof(object).IsAssignableFrom(typeof(string))\n  Foo();\n</code></pre>\n\n<p>See <a href="https://stackoverflow.com/questions/72360/how-to-use-the-is-operator-in-systemtype-variables">this question about types</a> for more details. Maybe it\'ll help you.</p>\n'}, {'answer_id': 75266, 'author': 'Paul van Brenk', 'author_id': 1837197, 'author_profile': 'https://Stackoverflow.com/users/1837197', 'pm_score': -1, 'selected': False, 'text': '<p>You want to check out the <a href="http://msdn.microsoft.com/en-us/library/system.type.isinstanceoftype.aspx" rel="nofollow noreferrer">Type.IsInstanceOfType</a> method</p>\n'}, {'answer_id': 75341, 'author': 'Thomas Danecker', 'author_id': 9632, 'author_profile': 'https://Stackoverflow.com/users/9632', 'pm_score': 1, 'selected': False, 'text': "<p>I'd use overloading:</p>\n\n<pre><code>public static void DoSomething&lt;K,V&gt;(IDictionary&lt;K,V&gt; items)\n  where V : IEnumerable\n{\n   items.Keys.Each(key =&gt; { /* do something */ });\n}\n\npublic static void DoSomething&lt;K,V&gt;(IDictionary&lt;K,V&gt; items)\n{\n   items.Keys.Each(key =&gt; { /* do something else */ });\n}\n</code></pre>\n"}, {'answer_id': 75502, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 6, 'selected': True, 'text': '<p><a href="https://stackoverflow.com/a/74648/1968">The previously accepted answer</a> is nice but it is wrong. Thankfully, the error is a small one. Checking for <code>IEnumerable</code> is not enough if you really want to know about the generic version of the interface; there are a lot of classes that implement only the nongeneric interface. I\'ll give the answer in a minute. First, though, I\'d like to point out that the accepted answer is overly complicated, since the following code would achieve the same under the given circumstances:</p>\n\n<pre><code>if (items[key] is IEnumerable)\n</code></pre>\n\n<p>This does even more because it works for each item separately (and not on their common subclass, <code>V</code>).</p>\n\n<p>Now, for the correct solution. This is a bit more complicated because we have to take the generic type <code>IEnumerable`1</code> (that is, the type <code>IEnumerable&lt;&gt;</code> with one type parameter) and inject the right generic argument:</p>\n\n<pre><code>static bool IsGenericEnumerable(Type t) {\n    var genArgs = t.GetGenericArguments();\n    if (genArgs.Length == 1 &amp;&amp;\n            typeof(IEnumerable&lt;&gt;).MakeGenericType(genArgs).IsAssignableFrom(t))\n        return true;\n    else\n        return t.BaseType != null &amp;&amp; IsGenericEnumerable(t.BaseType);\n}\n</code></pre>\n\n<p>You can test the correctness of this code easily:</p>\n\n<pre><code>var xs = new List&lt;string&gt;();\nvar ys = new System.Collections.ArrayList();\nConsole.WriteLine(IsGenericEnumerable(xs.GetType()));\nConsole.WriteLine(IsGenericEnumerable(ys.GetType()));\n</code></pre>\n\n<p>yields:</p>\n\n<pre><code>True\nFalse\n</code></pre>\n\n<p>Don\'t be overly concerned by the fact that this uses reflection. While it\'s true that this adds runtime overhead, so does the use of the <code>is</code> operator.</p>\n\n<p>Of course the above code is awfully constrained and could be expanded into a more generally applicable method, <code>IsAssignableToGenericType</code>. The following implementation is slightly incorrect<sup>1</sup> and I’ll leave it here <em>for historic purposes only</em>. <strong>Do not use it</strong>. Instead, <a href="https://stackoverflow.com/a/1075059/1968">James has provided an excellent, correct implementation in his answer.</a></p>\n\n<pre><code>public static bool IsAssignableToGenericType(Type givenType, Type genericType) {\n    var interfaceTypes = givenType.GetInterfaces();\n\n    foreach (var it in interfaceTypes)\n        if (it.IsGenericType)\n            if (it.GetGenericTypeDefinition() == genericType) return true;\n\n    Type baseType = givenType.BaseType;\n    if (baseType == null) return false;\n\n    return baseType.IsGenericType &amp;&amp;\n        baseType.GetGenericTypeDefinition() == genericType ||\n        IsAssignableToGenericType(baseType, genericType);\n}\n</code></pre>\n\n<p><sup>1</sup> It fails when the <code>genericType</code> is the same as <code>givenType</code>; for the same reason, it fails for nullable types, i.e.</p>\n\n<pre><code>IsAssignableToGenericType(typeof(List&lt;int&gt;), typeof(List&lt;&gt;)) == false\nIsAssignableToGenericType(typeof(int?), typeof(Nullable&lt;&gt;)) == false\n</code></pre>\n\n<p>I’ve created a <a href="https://gist.github.com/4174727" rel="noreferrer">gist with a comprehensive suite of test cases</a>.</p>\n'}, {'answer_id': 75600, 'author': 'Rich Visotcky', 'author_id': 400730, 'author_profile': 'https://Stackoverflow.com/users/400730', 'pm_score': 3, 'selected': False, 'text': '<p>A word of warning about generic types and using IsAssignableFrom()...</p>\n\n<p>Say you have the following:</p>\n\n<pre><code>public class MyListBase&lt;T&gt; : IEnumerable&lt;T&gt; where T : ItemBase\n{\n}\n\npublic class MyItem : ItemBase\n{\n}\n\npublic class MyDerivedList : MyListBase&lt;MyItem&gt;\n{\n}\n</code></pre>\n\n<p>Calling IsAssignableFrom on the base list type or on the derived list type will return false, yet clearly <code>MyDerivedList</code> inherits <code>MyListBase&lt;T&gt;</code>. (A quick note for Jeff, generics absolutely <em>must</em> be wrapped in a code block or tildes to get the <code>&lt;T&gt;</code>, otherwise it\'s omitted.  Is this intended?)  The problem stems from the fact that <code>MyListBase&lt;MyItem&gt;</code> is treated as an entirely different type than <code>MyListBase&lt;T&gt;</code>.  The following article could explain this a little better.  <a href="http://mikehadlow.blogspot.com/2006/08/reflecting-generics.html" rel="nofollow noreferrer">http://mikehadlow.blogspot.com/2006/08/reflecting-generics.html</a></p>\n\n<p>Instead, try the following recursive function:</p>\n\n<pre><code>    public static bool IsDerivedFromGenericType(Type givenType, Type genericType)\n    {\n        Type baseType = givenType.BaseType;\n        if (baseType == null) return false;\n        if (baseType.IsGenericType)\n        {\n            if (baseType.GetGenericTypeDefinition() == genericType) return true;\n        }\n        return IsDerivedFromGenericType(baseType, genericType);\n    }\n</code></pre>\n\n<p>/EDIT: Konrad\'s new post which takes the generic recursion into account as well as interfaces is spot on.  Very nice work.  :)</p>\n\n<p>/EDIT2: If a check is made on whether genericType is an interface, performance benefits could be realized.  The check can be an if block around the current interface code, but if you\'re interested in using .NET 3.5, a friend of mine offers the following:</p>\n\n<pre><code>    public static bool IsAssignableToGenericType(Type givenType, Type genericType)\n    {\n        var interfaces = givenType.GetInterfaces().Where(it =&gt; it.IsGenericType).Select(it =&gt; it.GetGenericTypeDefinition());\n        var foundInterface = interfaces.FirstOrDefault(it =&gt; it == genericType);\n        if (foundInterface != null) return true;\n\n        Type baseType = givenType.BaseType;\n        if (baseType == null) return false;\n\n        return baseType.IsGenericType ?\n            baseType.GetGenericTypeDefinition() == genericType :\n            IsAssignableToGenericType(baseType, genericType);\n    }\n</code></pre>\n'}, {'answer_id': 390981, 'author': 'Hosam Aly', 'author_id': 41283, 'author_profile': 'https://Stackoverflow.com/users/41283', 'pm_score': 0, 'selected': False, 'text': '<p>I used to think such a situation may be solvable in a way similar to @Thomas Danecker\'s <a href="https://stackoverflow.com/questions/74616/how-to-detect-if-type-is-another-generic-type#75341">solution</a>, but adding another template argument:</p>\n\n<pre><code>public static void DoSomething&lt;K, V, U&gt;(IDictionary&lt;K,V&gt; items)\n    where V : IEnumerable&lt;U&gt; { /* do something */ }\npublic static void DoSomething&lt;K, V&gt;(IDictionary&lt;K,V&gt; items)\n                             { /* do something else */ }\n</code></pre>\n\n<p>But I noticed now that it does\'t work unless I specify the template arguments of the first method explicitly. This is clearly not customized per each item in the dictionary, but it may be a kind of poor-man\'s solution.</p>\n\n<p>I would be very thankful if someone could point out anything incorrect I might have done here.</p>\n'}, {'answer_id': 1075059, 'author': 'James Fraumeni', 'author_id': 132345, 'author_profile': 'https://Stackoverflow.com/users/132345', 'pm_score': 7, 'selected': False, 'text': "<p>Thanks very much for this post. I wanted to provide a version of Konrad Rudolph's solution that has worked better for me. I had minor issues with that version, notably when testing if a Type is a nullable value type:</p>\n\n<pre><code>public static bool IsAssignableToGenericType(Type givenType, Type genericType)\n{\n    var interfaceTypes = givenType.GetInterfaces();\n\n    foreach (var it in interfaceTypes)\n    {\n        if (it.IsGenericType &amp;&amp; it.GetGenericTypeDefinition() == genericType)\n            return true;\n    }\n\n    if (givenType.IsGenericType &amp;&amp; givenType.GetGenericTypeDefinition() == genericType)\n        return true;\n\n    Type baseType = givenType.BaseType;\n    if (baseType == null) return false;\n\n    return IsAssignableToGenericType(baseType, genericType);\n}\n</code></pre>\n"}, {'answer_id': 8684023, 'author': 'Matt Johnson-Pint', 'author_id': 634824, 'author_profile': 'https://Stackoverflow.com/users/634824', 'pm_score': 2, 'selected': False, 'text': "<p>Thanks for the great info.  For convienience, I've refactored this into an extension method and reduced it to a single statement.</p>\n\n<pre><code>public static bool IsAssignableToGenericType(this Type givenType, Type genericType)\n{\n  return givenType.GetInterfaces().Any(t =&gt; t.IsGenericType &amp;&amp; t.GetGenericTypeDefinition() == genericType) ||\n         givenType.BaseType != null &amp;&amp; (givenType.BaseType.IsGenericType &amp;&amp; givenType.BaseType.GetGenericTypeDefinition() == genericType ||\n                                        givenType.BaseType.IsAssignableToGenericType(genericType));\n}\n</code></pre>\n\n<p>Now it can be easily called with:</p>\n\n<p>sometype.IsAssignableToGenericType(typeof(MyGenericType&lt;>))</p>\n"}]
['https://Stackoverflow.com/questions/74616', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12934/']