1 | #Region "Microsoft.VisualBasic::742fcc2538ea985c6b38aefbb6fe91f9, Microsoft.VisualBasic.Core\Scripting\Runtime\OverloadsFunction.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Class OverloadsFunction |
35 | ' |
36 | ' Properties: Name |
37 | ' |
38 | ' Constructor: (+1 Overloads) Sub New |
39 | ' Function: Align, Match, ToString |
40 | ' |
41 | ' |
42 | ' /********************************************************************************/ |
43 | |
44 | #End Region |
45 | |
46 | Imports System.Reflection |
47 | Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic |
48 | Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel.Repository |
49 | Imports Microsoft.VisualBasic.Linq |
50 | |
51 | Namespace Scripting.Runtime |
52 | |
53 | Public Class OverloadsFunction |
54 | Implements INamedValue |
55 | |
56 | Public Property Name As String Implements IKeyedEntity(Of String).Key |
57 | |
58 | ReadOnly functions As MethodInfo() |
59 | |
60 | Sub New(name$, methods As IEnumerable(Of MethodInfo)) |
61 | Me.Name = name |
62 | Me.functions = methods.ToArray |
63 | End Sub |
64 | |
65 | Public Function Match(args As Type()) As MethodInfo |
66 | Dim alignments = functions.Select(Function(m) Align(m, args)).ToArray |
67 | Dim p = Linq.Which.Max(alignments) |
68 | |
69 | If alignments(p) <= 0 Then |
70 | Return Nothing |
71 | End If |
72 | |
73 | Dim method As MethodInfo = functions(p) |
74 | Return method |
75 | End Function |
76 | |
77 | ''' <summary> |
78 | ''' Find the best matched overloads function based on the input parameter |
79 | ''' </summary> |
80 | ''' <param name="target"></param> |
81 | ''' <param name="args"></param> |
82 | ''' <returns></returns> |
83 | Public Shared Function Align(target As MethodInfo, args As Type()) As Double |
84 | Dim params = target.GetParameters |
85 | |
86 | If args.Length > params.Length Then |
87 | Return -1 |
88 | ElseIf params.Length = args.Length AndAlso args.Length = 0 Then |
89 | Return 100000000 |
90 | End If |
91 | |
92 | Dim score# |
93 | Dim tmp% |
94 | |
95 | For i As Integer = 0 To args.Length - 1 |
96 | tmp = 1000 |
97 | |
98 | If Not args(i).IsInheritsFrom(params(i).ParameterType, False, tmp) Then |
99 | Return -1 ' 类型不符,则肯定不可以使用这个方法 |
100 | Else |
101 | score += (Short.MaxValue - tmp) |
102 | End If |
103 | Next |
104 | |
105 | Return score |
106 | End Function |
107 | |
108 | Public Overrides Function ToString() As String |
109 | Return $"{Name} (+{functions.Length} Overloads)" |
110 | End Function |
111 | End Class |
112 | End Namespace |