469,646 Members | 1,161 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

late binding and reflection

Hi all,

what is the difference between the late binding and reflection?

clara
--
thank you so much for your help
Nov 13 '08 #1
4 2411
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/Reflect...mputer_science)

http://en.wikipedia.org/wiki/Late_binding but I prefer
http://en.wikipedia.org/wiki/Dynamic_dispatch that 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...

--
Patrice
"clara" <cl***@discussions.microsoft.coma écrit dans le message de groupe
de discussion : C0**********************************@microsoft.com...
Hi all,

what is the difference between the late binding and reflection?

clara
--
thank you so much for your help

Nov 13 '08 #2
On Nov 13, 2:12*pm, "Patrice" <http://www.chez.com/scribe/wrote:
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/Reflect...mputer_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...

--
Patrice

"clara" <cl...@discussions.microsoft.coma écrit dans le message de groupe
de discussion : C0154300-4666-4FC2-94DC-6E3D9A2DD...@microsoft.com...
Hi all,
what is the difference between the late binding and reflection?
clara
--
thank you so much for your help
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/
Nov 13 '08 #3
Hi Patrice,

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

Clara

--
thank you so much for your help
"Patrice" wrote:
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/Reflect...mputer_science)

http://en.wikipedia.org/wiki/Late_binding but I prefer
http://en.wikipedia.org/wiki/Dynamic_dispatch that 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...

--
Patrice
"clara" <cl***@discussions.microsoft.coma crit dans le message de groupe
de discussion : C0**********************************@microsoft.com...
Hi all,

what is the difference between the late binding and reflection?

clara
--
thank you so much for your help

Nov 13 '08 #4

"clara" <cl***@discussions.microsoft.comwrote in message
news:68**********************************@microsof t.com...
Hi Patrice,

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

Clara

--
thank you so much for your help
"Patrice" wrote:
>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/Reflect...mputer_science)

http://en.wikipedia.org/wiki/Late_binding but I prefer
http://en.wikipedia.org/wiki/Dynamic_dispatch that 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...

--
Patrice
"clara" <cl***@discussions.microsoft.coma crit dans le message de
groupe
de discussion : C0**********************************@microsoft.com...
Hi all,

what is the difference between the late binding and reflection?

clara
--
thank you so much for your help

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
Nov 13 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Scott English | last post: by
9 posts views Thread by John Smith | last post: by
5 posts views Thread by CodeRazor | last post: by
3 posts views Thread by Thomas Müller-Lynch | last post: by
30 posts views Thread by lgbjr | last post: by
4 posts views Thread by Rippo | last post: by
1 post views Thread by nelsonivan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.