How to grab every class from specified namespace c#? -
so have read few ver similar questions, answers not giving me result.
my code
list<type> thelist = assembly.getexecutingassembly().gettypes().tolist().where(t => t.namespace == "foo.test").tolist();
and not return anything, though there multiple classes inside namespace. there 3 static classes inside foo.test testone, testtwo, , testthree; none returned in little 1 liner got stack overflow thread.
how grab these 3 static classes given namespace foo.test? program namespace foo.program, both our namespaces close, , have using foo.test in program includes.
you're running both case sensitivity , incomplete namespace name issues. might better off using case insensitive search coupled indexof()
current culture.
list<type> thelist = assembly.getexecutingassembly().gettypes().where(t => culture.compareinfo.indexof(t.namespace, "foo.test", compareoptions.ignorecase)) .tolist();
alternatively, if namespace correct, can perform case insensitive search namespace.
list<type> thelist = assembly.getexecutingassembly().gettypes() .where(t => t.namespace.equals("foo.test", stringcomparison.currentcultureignorecase)) .tolist();
finally, it's worth noting load types executing assembly! if types live in external assembly, code not retrieve them! if had referenced class in code, forcing loaded app domain, iterate on loaded assemblies types.
list<type> thelist = new list<type>(); foreach (assembly in appdomain.currentdomain.getassemblies()) { thelist.addrange( a.gettypes() .where(t => t.namespace.equals("foo.test", stringcomparison.currentcultureignorecase)) .tolist()); }
Comments
Post a Comment