0

Possible Duplicate:
In there a generic constructor with parameter constraint in C#

I have this code that passes the object type dynamically:

public static void Retion<T>() where T : DataContext, new()
{
    using (T entitiesContext = new T())
    {...}

My problem is that I need a constructor with parameters, like this:

   public static void Retion<T>(String conn) where T : DataContext, new()
    {
        using (T entitiesContext = new T(conn))
        {...}

When I try this I get an error: Error 137 'T': cannot provide arguments when creating an instance of a variable type.

Community
  • 1
  • 1
user1662812
  • 2,531
  • 4
  • 23
  • 27
  • 1
    There's no guarantee that the actual type of T have a constructor with this signature, even if the Datacontext have it. That's why the constraints cannot be used when using parameters. – Steve B Sep 19 '12 at 15:03
  • @SteveB for that matter, there is no guarantee the actual type would have an empty constructor as well, but its enforced by constraints. I dont see a difference. This feature (what OP is asking) will be really handy – nawfal May 27 '13 at 03:39

1 Answers1

1

try

using (T entitiesContext = (T)Activator.CreateInstance(typeof(T), new[]{conn}))
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122