[Mono,C#] dynamic generic where new() >> InternalErrorException, NotImplementedExcept

after i finished writing a generic object manager and tabbed back into unity i get an exception from mono, narrowing down the problem the cause is a construct analogous to this

	class GenericObjectPool<T> where T : new() {
	}

	class ObjectLibrary {
		private Dictionary<Type, GenericObjectPool<dynamic>> _objectPools;
	}

specifically the requirement for a default constructor new() causes the error, without the “where T : new” clause mono doesn’t complain, other clauses like “where T : class” also work

the exact exception is at the bottom of the post

did anyone else have a similar problem? i realize this is a mono problem and not directly related to unity, but i need the new() thing so i can instantiate a new object in the pool if there are no more objects to recycle

i guess i could do a workaround with something like

interface IObjectCreator<T> {
		T CreateDefault();
	}

and for every new unique object type i use in the game provide an implementation of that interface – but that sucks

any ideas guys? thanks

What’s on line 11 of ObjectLibrary/Test.cs that’s throwing the NotImplementedException?

Also - what’re you typically passing as T in instances of GenericObjectPool? Just wondering if you’re bumping up against the MonoBehaviour constructor limitation in Unity.

it’s the

private Dictionary<Type, GenericObjectPool<dynamic>> _objectPools;

statement

i am not passing anything
the script won’t compile at all, there are no errors in visual studio (or monodevelop) so it is syntactically valid c# but when i tab to unity the script compilation process throws this error
apparently the mono version that unity uses does not support dynamic generic types with new() constraints

but what i originally wanted my class to do was stuff like

// get a list by specifying a generic type and let the generic object manager create/figure out the pools and return a recycled or new one
var list = Library.Get<List<MyComponent>>();
// do stuff with the list
foreach(...) {...}
// clear to allow garbage collection
list.Clear();
// put it back into the generic object manager and let it figure out which object pool to return the list to
Library.Put(list);

dynamic is part of .NET 4 which almost certainly means it’s not part of the Mono implementation that Unity uses (I’m guessing, as I haven’t actually tried to use it).

That being said - why it throws a NotImplementedException is beyond me. Unless it was stubbed out for some reason.

However, if MyComponent is a UnityEngine.Component derivative I’d still question the new() constraint in the first place.