Lista delle classi in un assieme
-
29-08-2019 - |
Domanda
Ho un gruppo di DLL, in cui ci sono varie classi. Ogni classe ha circa 50-100 membri e 4-5 funzioni. Come posso creare un elenco di tutte le classi ed i loro rispettivi membri che utilizzano un programma di VB.NET?
ho bisogno di mostrare all'utente per l'esecuzione di un'operazione che utilizza una classe particolare.
Soluzione
Vedere la documentazione per System.Reflection.Assembly .GetTypes () System.Type.GetMembers ()
- larsw
Altri suggerimenti
Supponendo che hai caricato l'assembly a thisAsm (in questo ex sto usando l'assemblea in esecuzione),
Questo ti porterà tutte le classi non astratte:
Assembly thisAsm = Assembly.GetExecutingAssembly();
List<Type> types = thisAsm.GetTypes().Where(t => t.IsClass && !t.IsAbstract).ToList();
E questo ti porterà tutte le classi che implementa un'interfaccia specifica.
(es. Se avete bisogno di ottenere solo le classi che implementa IYourInterface, quindi)
Assembly thisAsm = Assembly.GetExecutingAssembly();
List<Type> types = thisAsm.GetTypes().Where
(t => ((typeof(IYourInterface).IsAssignableFrom(t)
&& t.IsClass && !t.IsAbstract))).ToList();
Una volta che hai questo elenco di elementi, è possibile mostrare i membri di ogni tipo, chiamando i GetProperties () e GetMethods () su ogni membro della lista tipi.
È possibile ottenere tutti i tipi che eredita da Modulo in VB.net:
Dim thisAsm As Assembly = Assembly.GetExecutingAssembly()
Dim types As List(Of Type) = thisAsm.GetTypes().Where(Function(t) t.BaseType = GetType(Form)).ToList()
Questa è la versione vb.net in base alla risposta @amazedsaint:
Dim thisAsm As Assembly = Assembly.GetExecutingAssembly()
Dim types As List(Of Type) = thisAsm
.GetTypes()
.Where(Function(t) t.IsClass AndAlso Not t.IsAbstract).ToList()
Dim thisAsm As Assembly = Assembly.GetExecutingAssembly()
Dim types As List(Of Type) = thisAsm
.GetTypes()
.Where(Function(t) ((GetType(IYourInterface).IsAssignableFrom(t) AndAlso t.IsClass AndAlso Not t.IsAbstract))).ToList()