I am trying to do the following:
class Program
{
static void Main(string[] args)
{
foo<baz> fooObject = new foo<baz>();
AnotherClass<baz> _baz = new AnotherClass<baz>();
_baz.testMethod(fooObject);
}
}
public class AnotherClass<T> where T : bar
{
public void testMethod(foo<T> dummy)
{
foobar = dummy;
}
private foo<T> foobar = null;
}
public class foo<T> where T : bar, new()
{
public foo()
{
_t = new T();
}
private T _t;
}
public abstract class bar
{
public abstract void someMethod();
// Some implementation
}
public class baz : bar
{
public override void someMethod()
{
//Implementation
}
}
And I get an error explaining that 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method. I fully understand why this must be, and also understand that I could pass a pre-initialized object of type 'T' in as a constructor argument to avoid having to 'new' it, but is there any way around this? any way to enforce classes that derive from 'bar' to supply parameterless constructors?
Fixed - I was missing a 'new()' constraint on AnotherClass().