title: Casting to abstract class or interface when generics are used
body: <p>I have this method Verify_X which is called during databind for a listbox selected value.  The problem is the strongly typed datasource.  I want to use the abstract class BaseDataSource or an interface to call the methods supported:
  Parameters[] and Select(), Instead of using the most specific implementation as seen below.</p>
  
  <p>This is so one method can be used for all the different types of datasources I have instead of having a method for each.  They all inherit the same way.</p>
  
  <p>Here is the chain of inheritance / implementation</p>
  
  <pre><code>public class DseDataSource : ProviderDataSource&lt;SCCS.BLL.Dse, DseKey&gt;
  
  public abstract class ProviderDataSource&lt;Entity, EntityKey&gt; : BaseDataSource&lt;Entity, EntityKey&gt;, ILinkedDataSource, IListDataSource
      where Entity : SCCS.BLL.IEntityId&lt;EntityKey&gt;, new()
      where EntityKey : SCCS.BLL.IEntityKey, new()
  
  public abstract class BaseDataSource&lt;Entity, EntityKey&gt; : DataSourceControl, IListDataSource, IDataSourceEvents
      where Entity : new()
      where EntityKey : new()
  </code></pre>
  
  <p>The BaseDataSource has the methods and properties I need. DseDataSource is implemented the following way:</p>
  
  <pre><code>public class DseDataSource : ProviderDataSource&lt;SCCS.BLL.Dse, DseKey&gt;
  </code></pre>
  
  <p>I know it is possible to edit the class DseDataSource, add an interface to access Parameters[] and Select(), then program against that, which allows what I want, but this requires editing the NetTiers libraries and I am curious to see if this can be done since it seemed so difficult.</p>
  
  <pre><code>    public static string Verify_DSE(string valueToBind, DseDataSource dataSource)
      {
          if (ListContainsValue(dataSource.GetEntityList(), valueToBind)) return valueToBind;
          CustomParameter p = dataSource.Parameters["WhereClause"] as CustomParameter;
          if (p != null)
          {
              p.Value = "IsActive=true OR Id=" + valueToBind;
              dataSource.Select();
              return valueToBind;
          }
          return string.Empty;
      }
  
      private static bool ListContainsValue(IEnumerable list, string value)
      {
          if (value.Length == 0) return true;
  
          foreach (object o in list)
          {
              IEntity entity = o as IEntity;
              if (entity != null)
              {
                  if (entity.Id.ToString() == value)
                      return true;
              }
          }
          return false;
      }
  </code></pre>
  
  <p>The end result would be code such as:</p>
  
  <pre><code>public static string Verify(string valueToBind, object dataSource)
  {
  //what is the correct way to convert from object
  BaseDataSource baseInstance = dataSource as BaseDataSource;
  
  if baseInstance != null)
  {
      if (ListContainsValue(baseInstance.GetEntityList(), valueToBind)) return valueToBind;
      CustomParameter p = baseInstance.Parameters["WhereClause"] as CustomParameter;
      if (p != null)
      {
          p.Value = "IsActive=true OR Id=" + valueToBind;
          baseInstance.Select();
          return valueToBind;
      }
  }
  
  return string.Empty;
  }
  </code></pre>