From 82e094946e657aa1d03a920157aeacb2db543e2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=93=E3=81=AE=E4=B8=AD=E4=BA=8C=E7=97=85=E3=81=AB?= =?UTF-8?q?=E7=88=86=E7=84=94=E3=82=92=EF=BC=81?= Date: Tue, 26 Jan 2021 10:19:39 +0800 Subject: [PATCH] improvements for handle lambda function --- Parallel/IpcParallel/IDelegate.vb | 35 ++++++++++++++++++++++++----- Parallel/IpcParallel/SlaveTask.vb | 6 ++--- Parallel/IpcParallel/TaskBuilder.vb | 5 +++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Parallel/IpcParallel/IDelegate.vb b/Parallel/IpcParallel/IDelegate.vb index 350cb97..f2efc8a 100644 --- a/Parallel/IpcParallel/IDelegate.vb +++ b/Parallel/IpcParallel/IDelegate.vb @@ -1,4 +1,5 @@ Imports System.Reflection +Imports Microsoft.VisualBasic.Scripting.SymbolBuilder #If netcore5 = 1 Then Imports Microsoft.VisualBasic.ApplicationServices.Development.NetCore5 #End If @@ -27,18 +28,40 @@ Public Class IDelegate Call Me.New(target.Method) End Sub + Public Function GetMethodTarget() As Object + Dim type As Type = provideType() + + If VBLanguage.IsValidVBSymbolName(name) Then + Return Nothing + Else + Return Activator.CreateInstance(type) + End If + End Function + + Private Function provideType() As Type + Return type.GetType(knownFirst:=True, searchPath:={filepath}) + End Function + Public Function GetMethod() As MethodInfo - Dim type As Type = Me.type.GetType(knownFirst:=True, searchPath:={filepath}) + Dim type As Type = provideType() #If netcore5 = 1 Then Call deps.TryHandleNetCore5AssemblyBugs(package:=type) #End If - For Each method As MethodInfo In CType(type, System.Reflection.TypeInfo).DeclaredMethods - If method.IsStatic AndAlso method.Name = name Then - Return method - End If - Next + If Not VBLanguage.IsValidVBSymbolName(name) Then + For Each method As MethodInfo In CType(type, System.Reflection.TypeInfo).DeclaredMethods + If (Not method.IsStatic) AndAlso method.Name = name Then + Return method + End If + Next + Else + For Each method As MethodInfo In CType(type, System.Reflection.TypeInfo).DeclaredMethods + If method.IsStatic AndAlso method.Name = name Then + Return method + End If + Next + End If Return Nothing End Function diff --git a/Parallel/IpcParallel/SlaveTask.vb b/Parallel/IpcParallel/SlaveTask.vb index ed9ad4e..eb24858 100644 --- a/Parallel/IpcParallel/SlaveTask.vb +++ b/Parallel/IpcParallel/SlaveTask.vb @@ -57,7 +57,7 @@ Public Class SlaveTask End If End Function - Public Function RunTask(entry As [Delegate], ParamArray parameters As Object()) As Object + Public Function RunTask(Of T)(entry As [Delegate], ParamArray parameters As Object()) As T Dim target As New IDelegate(entry) Dim result As Object = Nothing Dim host As IPCSocket = Nothing @@ -78,8 +78,8 @@ Public Class SlaveTask Dim commandlineArgvs As String = builder(processor, host.HostPort) 'If Not debugPort Is Nothing Then - ' Console.WriteLine(commandlineArgvs) - ' Pause() + ' Console.WriteLine(commandlineArgvs) + ' Pause() 'End If #If netcore5 = 0 Then diff --git a/Parallel/IpcParallel/TaskBuilder.vb b/Parallel/IpcParallel/TaskBuilder.vb index a53cf03..1e2e615 100644 --- a/Parallel/IpcParallel/TaskBuilder.vb +++ b/Parallel/IpcParallel/TaskBuilder.vb @@ -23,6 +23,7 @@ Public Class TaskBuilder : Implements ITaskDriver Public Function Run() As Integer Implements ITaskDriver.Run Dim task As IDelegate = GetMethod() Dim api As MethodInfo = task.GetMethod + Dim target As Object = task.GetMethodTarget Dim n As Integer = GetArgumentValueNumber() Dim args As New List(Of Object) @@ -45,8 +46,8 @@ Public Class TaskBuilder : Implements ITaskDriver ' send debug message Call New TcpRequest(masterPort).SendMessage(New RequestStream(IPCSocket.Protocol, Protocols.PostStart)) - Call PostFinished(api.Invoke(Nothing, args.ToArray)) - ' Call PostStdOut(api.Invoke(Nothing, args.ToArray)) + Call PostFinished(api.Invoke(target, args.ToArray)) + ' Call PostStdOut(api.Invoke(target, args.ToArray)) Call Console.WriteLine("job done!") Return 0