Joe Marcavage

Joe Marcavage

  • NA
  • 1
  • 2.3k

C#.NET Parameterizing a Generic

Jul 19 2010 11:48 AM
Goal:  Genericize the method so I can pass in a database wrapper class that takes a "functions" class as a parameter.  The "functions" class has all the connection plumbing, etc.  The caller to GetWrapper() has knowledge of what database wrapper to use, but doesn't know what the underlying database architecture is.

Error Message:  The type parameter 'T' cannot be used with type arguments.


Is there ever a case when I can do something like the below:

      public static T GetWrapper<T>(string provider)
        {
            T wrap = default(T);

            switch (provider)
            {
                case "System.Data.SqlClient":
                    wrap = new T<SQLServerFunctions>();
                    break;
                case "System.Data.Odbc":
                    wrap = new T<PostgresFunctions>();
                    break;
                case "MySql.Data.MySqlClient":
                    wrap = new T<MySQLFunctions>();
                    break;
            }
            return wrap;
        }


I'm thinking the answer is "no" but I wanted to ask the experts here.  Thanks in advance. 
Joe M.

Answers (1)