late binding and reflection

  • Thread starter Thread starter clara
  • Start date Start date
Reflection is the mechanism that allows to get information on a type and
other elements at runtime :

MsgBox(GetType(String).GetMembers.Count)

gets information about the string type...

Late binding is resolving a call at runtime (and in particular calling a
method on an object whose type is not known at all at compile time i.e. on a
"As Object" variable).

  Dim obj As Object
        obj = CreateObject("WScript.Network")
        MsgBox(obj.UserName)
        MsGbox(obj.ThisOneIsLikelyNotWorking)

If I change the "WScript.Network" string the first call could work or not
depending on wether or not this class exposes a UserName property but I'll
know it only at runtime...

They are somewhat related (AFAIK for now C# can use Reflection to implement
late binding whihc is not yet available as a language feature as in VB).

Try wikipedia for details :http://en.wikipedia.org/wiki/Reflection_(computer_science)

http://en.wikipedia.org/wiki/Late_bindingbut I preferhttp://en.wikipedia.org/wiki/Dynamic_dispatchthat is IMO closer of how
"Late binding" is used in the context of Visual Basic...

In most cases, you'll want to avoid late binding and at a less degree
System.Reflection as much as you can...

Darn, by the time I dug up the articles and got over here, you already
posted them!

:-)

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
Hi Patrice,

Thank you very much!
My next question is usage of Reflection and especially how can we combine
Attribute with it?

Clara
 
clara said:
Hi Patrice,

Thank you very much!
My next question is usage of Reflection and especially how can we combine
Attribute with it?

Clara

Here are 3 snippits of code:

1st: Create Attribute

<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Method)> _
Public Class MyAttribute
Inherits System.Attribute

Private _myName As String

Public ReadOnly Property MyName() As String
Get
Return _myName
End Get
End Property

Public Sub New(ByVal myName As String)
_myName = myName
End Sub
End Class


2nd: Use Attribute in object property
Imports CreatingUserAttributes


Public Class MyObject

Private _firstProp As String

<MyAttribute("Prop")> _
Public Property FirstProp() As String
Get
Return _firstProp
End Get
Set(ByVal value As String)
_firstProp = value
End Set
End Property

<MyAttribute("Tester")> _
Public Sub OurFirstMethod()
Dim i As Integer = 1
End Sub

<MyAttribute("moreTesting")> _
Public Sub OurSecondMethod()
Dim t As Integer = 1
End Sub

End Class

3rd: Test for attribute using reflection
Imports System.Reflection

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim t As New MyObject

Dim typ As Type = t.GetType()

Dim props() As PropertyInfo = typ.GetProperties()
For Each p As PropertyInfo In props
Dim obj As Array = p.GetCustomAttributes(False)
For Each a As Attribute In obj
If a.TypeId Is GetType(MyAttribute) Then
Dim nam As String = String.Empty
Dim oo As MyAttribute = CType(a, MyAttribute)
End If
Next
Next
End Sub
End Class

Hope this helps
LS
 
Back
Top