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();
                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.

2 Years
Discussion Span
Last Post by DaveAmour

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.

Votes + Comments

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.