Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Validating on Overridable method
Message
De
07/09/2011 09:56:02
 
 
À
07/09/2011 09:32:56
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01522449
Message ID:
01522895
Vues:
39
This message has been marked as a message which has helped to the initial question of the thread.
>>Could be shorter (and safer):
Public Function IsDerivedMethod(o As Object, methodName As String) As Boolean
>>	Dim t As Type = o.[GetType]()
>>	Dim minfo As MethodInfo = t.GetMethod(methodName, BindingFlags.Instance Or BindingFlags.[Public])
>>
>>	If minfo Is Nothing Then
>>		Throw New Exception("Method name does not exist")
>>	End If
>>
>>	While t.BaseType <> GetType([Object])
>>		t = t.BaseType
>>	End While
>>	Return minfo.DeclaringType <> t
>>End Function
>>
>
>Thanks, in my case, it gives this:
>
>
>        ' If a method exists in a sub class. This is used when we negotiate with the Overrides method. Thus, the framework
>        ' may know if the Overrides method exists in the sub class. This is used in the DataEntry class. So, if the method
>        ' does not exist, we do not call it. And, if it exists, it allows us to validate, if necessary, before proceeding.
>        ' expO1 Originator
>        ' expC1 Name of the method
>        Public Function IsMethodExistInSubClass(ByVal toOriginator As Object, ByVal tcMethod As String) As Boolean
>            Dim llExist As Boolean = False
>            Dim loType As Type = Nothing
>            Dim loMethodInfo As System.Reflection.MethodInfo = Nothing
>
>            ' Get a reference to the originator type
>            loType = toOriginator.GetType()
>
>            ' Get a reference to the method
>            loMethodInfo = loType.GetMethod(tcMethod, System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.Public)
>
>            ' If the method does not exist
>            If loMethodInfo Is Nothing Then
>                Return False
>            End If
>
>            ' Loop until we have reached the base class
>            While loType <> GetType([Object])
>                loType = loType.BaseType
>            End While
>
>            ' If the method exists in the sub class
>            If loMethodInfo.DeclaringType <> loType Then
>                llExist = True
>            End If
>
>            Return llExist
>        End Function
>
>
>I still do not understand however why I cannot use the GetType("Object") and have to use the syntax GetType([Object]). What does this represent?

TBH I used C# to write this and used a converter to get the VB version. IIRC the usual reason to use square brackets around a string in VB is to avoid confusing the compiler when the text itself if a VB.NET keyword but I don't think it is actually neccessary in this context.....
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform