1 #Region "Microsoft.VisualBasic::b14bc5436ac6578fc064be651c3862a6, Microsoft.VisualBasic.Core\Extensions\Reflection\Delegate\TypeExtensions.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 TypeExtensions
35     
36     '         Function: CanBeAssignedFrom, ImplementInterface
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.Runtime.CompilerServices
44
45 Namespace Emit.Delegates
46
47     Public Module TypeExtensions
48
49         ''' <summary>
50         ''' <paramref name="source"/>能否转换至<paramref name="destination"/>类型?
51         ''' </summary>
52         ''' <param name="destination"></param>
53         ''' <param name="source"></param>
54         ''' <returns></returns>
55         <Extension>
56         Public Function CanBeAssignedFrom(destination As Type, source As Type) As Boolean
57             If source Is Nothing OrElse destination Is Nothing Then
58                 Return False
59             End If
60
61             If destination Is source OrElse source.IsSubclassOf(destination) Then
62                 Return True
63             End If
64
65             If destination.IsInterface Then
66                 Return source.ImplementInterface(destination)
67             End If
68
69             If Not destination.IsGenericParameter Then
70                 Return False
71             End If
72
73             Dim constraints = destination.GetGenericParameterConstraints()
74             Return constraints.All(Function(t1) t1.CanBeAssignedFrom(source))
75         End Function
76
77         ''' <summary>
78         ''' 目标类型<paramref name="source"/>是否实现了制定的接口类型?
79         ''' </summary>
80         ''' <param name="source"></param>
81         ''' <param name="interfaceType">接口类型信息</param>
82         ''' <returns></returns>
83         <Extension> Public Function ImplementInterface(source As Type, interfaceType As Type) As Boolean
84             While source IsNot Nothing
85                 Dim interfaces = source.GetInterfaces()
86                 If interfaces.Any(Function(i) i Is interfaceType OrElse i.ImplementInterface(interfaceType)) Then
87                     Return True
88                 End If
89
90                 source = source.BaseType
91             End While
92
93             Return False
94         End Function
95     End Module
96 End Namespace