title: Calling a generic function from inside a task?
body: <p>I'm trying to call a generic function from a different thread but for some reason my generic parameter T does not seem to be recognized from within the task. My generic function looks like this</p>
  
  <pre><code>   private T extractSingleItem&lt;T&gt;(NpgsqlDataReader dr) where T : new()
      {
          T c = new T();
          if (dr.Read())
          {
  
              foreach (PropertyInfo p in typeof(T).GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance))
              {
                  try
                  {
                      //extracting the column name for the property's attributes
                      string column = ((TableAttribute)p.GetCustomAttribute(typeof(TableAttribute))).Column;
                      c.GetType().GetProperty(p.Name).SetValue(c, dr[column]);
                  }
                  catch
                  {
  
                  }
              }
          }
  
          return c;
      }
  </code></pre>
  
  <p>and I am trying to call it like this</p>
  
  <pre><code>public override void execQuery&lt;T&gt;(string query, Dictionary&lt;string, dynamic&gt; qparams, EventHandler&lt;dynamic&gt; callBack) 
      {
  
  
          Task.Run(() =&gt; {
  
              string db = mConnection.Database;
              if (mConnection.State == ConnectionState.Closed)
                  mConnection.Open();
  
  
              NpgsqlCommand command = new NpgsqlCommand(query, mConnection);
  
              command.Parameters.AddRange(qparams.Select(x =&gt; new NpgsqlParameter(x.Key, x.Value)).ToArray());
  
              NpgsqlDataReader dr = null;
  
              try
              {
                  dr = command.ExecuteReader();
              }
              catch
              {
                  command.Connection = genNewConn();
                  dr = command.ExecuteReader();
  
              }
  
              var res = extractSingleItem&lt;T&gt;(dr);
  
          });
  
  
      }
  </code></pre>
  
  <p>but someone I get this error</p>
  
  <p>"Severity   Code    Description Project File    Line    Suppression State
  Error   CS0310  'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method" </p>
  
  <p>only when I call from within the task.</p>