How to find nested classes in VB.Net using reflection? -
i have next class :
public class f public class public class c end class end class public class b end class end class
and i'm writing function homecoming embedded classes of class f. i'm expecting function homecoming & b types...
public function findinternalclasses(byval tbasetype object) list(of type) dim basetype = tbasetype.gettype dim assembly = basetype.assembly dim output new list(of type) each item in assembly.gettypes if item.issubclassof(basetype) output.add(item) end if next homecoming output end function
when running function, homecoming nothing. (the status "if item.issubclassof(basetype)" false.)
does know please missing code ?
they nested types. nested types don't have inheritance relationship outer type issubclassof() cannot work. property makes them distinctive type.declaringtype property, references outer type. code ought like:
public function findnestedtypes(byval outertype type) list(of type) dim output new list(of type) each item in outertype.assembly.gettypes() dim declarer = item.declaringtype while declarer isnot nil if declarer outertype output.add(item) exit end if declarer = declarer.declaringtype loop next homecoming output end function
sample usage:
for each t type in findnestedtypes(gettype(f)) console.writeline(t.fullname) next
output:
consoleapplication1.f+a consoleapplication1.f+b consoleapplication1.f+a+c
if don't want find class c remove while loop.
vb.net reflection
No comments:
Post a Comment