title: How to loop through a list of a generic type
body: <p>I need to loop through a list where the type is not known at compile-time. How to do that? The following code fails at runtime, conversion not allowed:</p>
  
  <pre><code>    Type objType = dataObject.GetType();
      List&lt;string&gt; strList = new List&lt;string&gt;();
  
      foreach (PropertyInfo prop in objType.GetProperties())
      {
          var val = prop.GetValue(dataObject);
  
          if (prop.PropertyType.Name.StartsWith("List"))   // Is there a better way?
          {
              foreach (object lval in (List&lt;object&gt;) val)  // Runtime failure (conversion not allowed)
              {
                  strList.Add(lval.ToString());
              }
          }
          ...
  </code></pre>