Search code examples
vb.netreflectionlate-binding

vb.net reflection vs. late binding?


What should be more proper or what is recommended to use in VB.NET from either reflection vs. late binding:

'Type can be various objects that have a common property for sure.'
Dim type = sender.GetType()
Dim prop = type.GetProperty("Text", 20)
Dim value = property.GetValue(sender, Nothing)

versus:

Dim value = sender.Text

Solution

  • Under the covers they are both doing the same thing (relatively speaking). VB.NET's late-binding feature is done via assembly metadata queries at runtime which is exactly what reflection is all about.

    One of the benefits to your first approach is that you have an opportunity to handle errors in a more finely-grained manner.