Qual è il modo migliore per convertire una stringa in un oggetto Type in .NET?Il modo migliore per ottenere un oggetto Type da una stringa in .NET
Problemi da considerare:
- Il tipo può essere in un assembly diverso.
- L'assembly del tipo potrebbe non essere ancora caricato.
Questo è il mio tentativo, ma non affronta il secondo numero
Public Function FindType(ByVal name As String) As Type
Dim base As Type
base = Reflection.Assembly.GetEntryAssembly.GetType(name, False, True)
If base IsNot Nothing Then Return base
base = Reflection.Assembly.GetExecutingAssembly.GetType(name, False, True)
If base IsNot Nothing Then Return base
For Each assembly As Reflection.Assembly In _
AppDomain.CurrentDomain.GetAssemblies
base = assembly.GetType(name, False, True)
If base IsNot Nothing Then Return base
Next
Return Nothing
End Function
Affrontare il secondo caso è difficile. Come sapere genericamente dove risiedono gli assemblaggi scaricati? Altrimenti vedi [questa risposta] (http://stackoverflow.com/a/7286354/661933), abbastanza buono. – nawfal