c# - Instantiating a generic type from a different class -


i have 2 classes, hierarchy , folder, using model tree structure of folders in server. have used generic type, since wanted make bit more versatile. however, i've run problem.

i trying recursively navigate tree add new generic t element, following function:

public t additem (string path, int id)   {   t result = default(t);    if (this.path == path)     {     result = new t ();     items.add (id, result);     }    return result;   } 

but new t() doesn't work, unsurprisingly. error is:

cannot create instance of variable type 't' because not have new() constraint

i tried add new() constraint in base file (hierarchy), doesn't work in case, , not sure can now. there way instantiate generic object?

edit adding constraint method doesn't work either.

error: constraints not allowed on non-generic declarations

declare class below. enforce t have parameterless public constructor , you'll able write new t();

class myclass<t> t : new()  {     public t additem(string path, int id)     {        // code     } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -