title: C# Generics - Constraints on type parameters
body: <p>I'm trying to build a factory method that uses the generics feature of C#. </p>
  
  <p>In this factory method I would like to constraint it to some specific classes, all of which do not have a default constructor.</p>
  
  <p>Here is my example. Can someone tell me if it's possible to run it?</p>
  
  <pre><code>public class AbstractClass {
      //this abstract class does not have a default constructor, nor its subclasses
      public AbstractClass(SomeClassName obj) {
          //use obj for initialization
      }
  }
  
  //this factory class should create objects of type T that inherit 
  //from AbstractClass and invoke the non-default constructor
  public class FactoryClass {
      public static T BuildObject&lt;T&gt; (SomeClassName obj) where T: AbstractClass {
          return new T(obj); //does not work?!?!?!
      }
  }
  
  
  //Edit: ANSWER!!!
  public static T BuildObject&lt;T&gt;(SomeClassUsedForTheConstructor item) where T : SomeAbstractClass { 
  return (T) Activator.CreateInstance(typeof (T), item); 
  } 
  </code></pre>