Hello Everyone,

Actually i have one question. I create one generic method like below:-

public static dynamic ExecuteSelectProcedure<T>(string procedureName) where T: class
        {
            SqlDataReader reader = ExecuteSelectCommand(procedureName, CommandType.StoredProcedure);
            if (typeof(T).FullName.Equals("System.Data.DataTable"))
            {
                DataTable dt = new DataTable();
                dt.Load(reader);
                reader.Close();
                return dt;
            }
            return reader;
        }

Now i want to restrict user to give type name only SqlDataReader or DataTable for T
Is it possible?
Please give some idea.
Thank's

Recommended Answers

All 2 Replies

With generics, you can use type parameter constraints to limit the types someone can use with a method, to a certain extent.

If I'm reading you right, though, that's not what you want. The point of a generic method is that the code is identical, no matter what type you put in the parameter. If you find yourself making decisions based on what the actual type is, then you're probably misusing generics.

In this specific case, I think what you're really looking for is a pair of methods, perhaps like this:

public static SqlDataReader ExecuteSelectProcedureSqlDataReader (string procedureName);
public static DataTable ExecuteSelectProcedureDataTable(string procedureName);

To preserve the kind of reuse you're attempting, you can have ExecuteSelectProcedureDataTable call ExecuteSelectProcedureSqlDataReader internally.

commented: Great! +15

I was thinking alot about this post and had already come to the same conclusion as gusano79 so just adding my support for his answer, you are definatley misusing generics.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.