"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