Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Lazy Type Wrapper, dynamic invoking made easy

This is a very small yet powerful little invoker that wraps around a type and caches all methods found within that type.
As easy as calling them by name (String) with parameters that don't necessarily have to match, it is forgiven meaning
it will try to convert parameters that can be converted in the signature it requires.

The MethodAttributes parameter is there to filter those methods that are desired, such Shared and Public methods only.

This wrapper is part of a bigger project i'm working on but has many uses for other projects.

Remark: It does not support multiple signatures of the same method

Usage:
PHP Code

<?php 
dim Invoker 
as New Invoker(SomeType, MethodAttributes.Public Or MethodAttributes.Static)

Dim Result as object = Invoker.Invoke("MyFunction",Arg0,Arg1,Etc...) 

PHP Code

<?php 
Imports System
.Reflection
Public Class Invoker
    Inherits Dictionary
(Of String, MethodInfo)
    Public 
Property Type As Type
    Sub 
New(Type As Type, MethodAttributes As MethodAttributes)
        
Me.Type = Type
        
For Each m As MethodInfo In Me.Type.GetMethods.Where(Function(x) x.Attributes = MethodAttributes)
            
Me.Add(m.Name.ToLower, m)
        
Next
    End Sub
    
Public Function Invoke(Name As String, ParamArray Parameters() As Object) As Object
        
If (Me.ContainsKey(Name.ToLower)) Then
            
If (Me.HasSignature(Me(Name.ToLower), Parameters)) Then
                
Return Me(Name.ToLower).Invoke(Me.Type, Parameters)
            Else
                Return 
Me(Name.ToLower).Invoke(Me.Type, Me.TryConvertParamaters(Me(Name.ToLower), Parameters))
            
End If
        
End If
        Return New 
Object
    End 
Function
    Private Function 
TryConvertParamaters(Target As MethodInfo, Parameters() As Object) As Object()
        
Dim Params As New List(Of Object)
        If (
Parameters.Count = Target.GetParameters.Count) Then
            
For i As Integer = 0 To Parameters.Count - 1
                
If (Parameters(i).GetType IsNot Target.GetParameters(i).ParameterType) Then
                    Params
.Add(Convert.ChangeType(Parameters(i), Target.GetParameters(i).ParameterType))
                Else
                    
Params.Add(Parameters(i))
                
End If
            
Next
        End 
If
        Return 
Params.ToArray
    End 
Function
    Private Function 
HasSignature(Target As MethodInfo, Parameters() As Object) As Boolean
        
If (Parameters.Count <> Target.GetParameters.Count) Then Return False
        
For i As Integer = 0 To Parameters.Count - 1
            
If (Parameters(i).GetType IsNot Target.GetParameters(i).ParameterType) Then
                
Return False
            End 
If
        
Next
        
Return True
    End 
Function
End Class 
things like this should be in shared class Invoker.Invoke("MyFunction", Arg0, Arg1, ...)
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Feb 22, 2017 12:36 PM)HFDone Wrote:  things like this should be in shared class Invoker.Invoke("MyFunction", Arg0, Arg1, ...)

Wuuuuuuuuuuuuut ? nooooooooooo way!!! You are sooooooo smart. No seriously, knock it off lad, its annoying. 🤝
and i thought you learn something ... and changed your behaviour! why you getting mad so quickly you could just ignore it.
soooooooooooo why its not?
dont except ppl to reply as you want! 🤝
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(Feb 22, 2017 01:04 PM)HFDone Wrote:  and i thought you learn something ... and changed your behaviour! why you getting mad so quickly you could just ignore it.
soooooooooooo why its not?
dont except ppl to reply as you want! 🤝

You wot mate ?
This is actually very useful. Thanks for releasing this.
Was looking for something like this. Thx for the source.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by jBarrelNov 15, 2022 02:52 PM 8 325 Nov 15, 2022 02:52 PM
Last Post: HFDone
[C] by maryousJul 24, 2022 03:35 PM 0 160 Jul 24, 2022 03:35 PM
Last Post: maryous
[Python] by simmerApr 9, 2022 02:34 PM 5 245 Apr 9, 2022 02:34 PM
Last Post: simmer