Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Type with namespace restriction

Tags:

c#

generics

Everyone knows

public class Test<T> where T : SomeBaseClass

But is there a way to restrict T to classes that exists in a namespace like

public class Test<T> where T in SomeNamespace

Best regards

like image 287
kyjan Avatar asked Feb 25 '12 15:02

kyjan


2 Answers

No. There's no way to constrain a generic type to a namespace.

like image 171
Justin Niessner Avatar answered Nov 01 '22 23:11

Justin Niessner


Namespace constrains are not possible. Anyway it does not makes any sence because everyone can create classes which are located in the target namespace. It maybe would only make sence if you can restrict it to a specific assembly.

Would make more sence if you can restrict it to n types like the following lines( doesn't work ):

public T Create<T>() where T : { MyClass1, MyClass2 }

See constraints

http://msdn.microsoft.com/en-us/library/d5x73970.aspx

like image 37
Felix K. Avatar answered Nov 02 '22 01:11

Felix K.