c# Use Generic as method parameter

I have a BaseConfig class that is extended for different projects. The BaseConfig class contains methods that are common among any project we develop. BaseConfig is setup like this (just a snippet of the entire class):

    public abstract class BaseConfig<T> where T:BaseConfig<T>
    {
        #region Serialized Class Members

        /// <summary>
        /// The License key for this product
        /// </summary>
        public string LicenseKey;

        /// <summary>
        /// Database connection information
        /// </summary>
        public class Database
        {
            public String ServerName;
            public String DatabaseName;
            public bool IntegratedSecurity;
            public String UserName;
            public String Password;
            public String ConnectionStringOptions;
        }
        public Database DatabaseInfo;
....

We use this BaseConfig class in conjuntion with a config.xml file that contains information specific to the customer (like databse info).

I would like to create a public method of ConnectToSQL() that can be called by any class extending BaseConfig.

IMPORTANT: ConnectToSQL() will be in a class other than BaseConfig (something like SQLConnectionClass() but within the same project).

I need to setup a method like:

private SqlConnection ConnectToSQL(Configuration.BaseConfig<T>.Database dbInfo)

and then be called like:

SqlConnection _conn = ConnectToSQL(databaseInfo);

where databaseInfo comes from a class extending BaseConfig

However, SqlConnection() complains about 'Type or namespace type T cannot be found'.

Anyone know how I could setup this method?

Jon Skeet
people
quotationmark

It seems to me that Database shouldn't be within BaseConfig<T> anyway - it should be a top-level class. You don't really want different types for BaseConfig<Foo>.Database, BaseConfig<Bar>.Database etc, do you? That's the problem - those are two valid and different types, and your method parameter doesn't express which of them it's interested in.

I'd also strongly recommend using properties instead of those public fields - and probably making the Database type immutable.

people

See more on this question at Stackoverflow