1 #Region "Microsoft.VisualBasic::3b37d844a99d6529dd03c3d1f459dfe2, Microsoft.VisualBasic.Core\Extensions\Reflection\Methods.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     Module MethodsExtension
35     
36     '     Function: (+2 OverloadsAsLazy, (+2 OverloadsInvoke, (+2 OverloadsTryInvoke
37     
38     ' /********************************************************************************/
39
40 #End Region
41
42 Imports System.Linq.Expressions
43 Imports System.Reflection
44 Imports System.Runtime.CompilerServices
45
46 Public Module MethodsExtension
47
48     ''' <summary>
49     ''' 尝试将目标对象放入到函数指针之中来运行,运行失败的时候回返回<paramref name="default"/>默认值
50     ''' </summary>
51     ''' <typeparam name="T"></typeparam>
52     ''' <typeparam name="TOut"></typeparam>
53     ''' <param name="input"></param>
54     ''' <param name="proc"></param>
55     ''' <param name="[default]"></param>
56     ''' <returns></returns>
57     <MethodImpl(MethodImplOptions.AggressiveInlining)>
58     <Extension> Public Function TryInvoke(Of T, TOut)(proc As Func(Of T, TOut), input As T, Optional [default] As TOut = NothingAs TOut
59         Return New Func(Of TOut)(Function() proc(input)).TryInvoke([default])
60     End Function
61
62     <Extension> Public Function TryInvoke(Of T)(proc As Func(Of T), Optional [default] As T = NothingAs T
63         Try
64             Return proc()
65         Catch ex As Exception
66             Call App.LogException(ex)
67             Return [default]
68         End Try
69     End Function
70
71     <MethodImpl(MethodImplOptions.AggressiveInlining)>
72     <Extension>
73     Public Function AsLazy(Of T)(factory As Func(Of T)) As Lazy(Of T)
74         Return New Lazy(Of T)(factory)
75     End Function
76
77     <MethodImpl(MethodImplOptions.AggressiveInlining)>
78     <Extension>
79     Public Function AsLazy(Of T)(lambda As LambdaExpression) As Lazy(Of T)
80         Return DirectCast(lambda.Compile, Func(Of T)).AsLazy
81     End Function
82
83     ''' <summary>
84     ''' Invoke a static method without parameters
85     ''' </summary>
86     ''' <typeparam name="T"></typeparam>
87     ''' <param name="method"></param>
88     ''' <returns></returns>
89     <MethodImpl(MethodImplOptions.AggressiveInlining)>
90     <Extension>
91     Public Function Invoke(Of T)(method As MethodInfo) As T
92         Return method.Invoke(Nothing, Nothing)
93     End Function
94
95     ''' <summary>
96     ''' Invoke a static method without parameters
97     ''' </summary>
98     ''' <param name="method"></param>
99     ''' <returns></returns>
100     <MethodImpl(MethodImplOptions.AggressiveInlining)>
101     <Extension>
102     Public Function Invoke(method As MethodInfo) As Object
103         Return method.Invoke(Nothing, Nothing)
104     End Function
105 End Module