1
只反射加载上下文使您能够检查为其他平台或 .NET Framework 的其他版本编译的程序集。加载到此上下文中的代码只能检查,不能执行。这意味着无法创建对象,因为无法执行构造函数。因为该代码无法执行,所以不会自动加载依赖项。如果需要对依赖项进行检查,则必须自行加载。
2
3
将程序集加载到只反射加载上下文中
4
使用 ReflectionOnlyLoad(String) 方法重载可加载给定了显示名称的程序集,而使用 ReflectionOnlyLoadFrom 方法可加载给定了路径的程序集。如果该程序集为二进制文件映像,则使用 ReflectionOnlyLoad(array<Byte>[]()[]) 方法重载。
5
6
注意:
7
您不能使用只反射上下文从非执行上下文中的 .NET Framework 版本加载 mscorlib.dll 版本。
8
9
10
如果该程序集具有依赖项,ReflectionOnlyLoad 方法不会加载这些依赖项。如果需要对依赖项进行检查,则必须自行加载。
11
12
使用程序集的 ReflectionOnly 属性确定是否将该程序集加载到了只反射上下文中。
13
14
如果向该程序集或程序集中的类型应用了属性,则应通过使用 CustomAttributeData 类检查这些属性,以确保未尝试在只反射上下文中执行代码。使用 CustomAttributeData..::.GetCustomAttributes 方法的适当重载可获取表示应用于程序集、成员、模块或参数的属性的 CustomAttributeData 对象。
15
16
注意:
17
应用于该程序集或其内容的属性可能是在该程序集中定义的,也可能是在加载到只反射上下文中的另一个程序集中定义的。无法事先知道这些属性是在何处定义的。
18
19
20
示例
21
下面的代码示例演示如何检查应用于加载到只反射上下文中的程序集的属性。
22
23
该代码示例定义了一个带有两个构造函数和一个属性 (Property) 的自定义属性 (Attribute)。该属性可应用于程序集、在该程序集中声明的类型、该类型的方法以及该方法的参数。在执行时,该程序集将其本身加载到只反射上下文中,并显示应用到它及它包含的类型和成员的自定义属性的有关信息。
24
25
注意:
26
为简化该代码示例,该程序集自行完成加载和检查操作。通常情况下,不要在执行上下文和只反射上下文中加载同一程序集。
27
28
29
Visual Basic 复制代码
30
Imports System
31
Imports System.Reflection
32
Imports System.Collections.Generic
33
Imports System.Collections.ObjectModel
34
35
' The example attribute is applied to the assembly.
36
<Assembly:Example(ExampleKind.ThirdKind, Note:="This is a note on the assembly.")>
37
38
' An enumeration used by the ExampleAttribute class.
39
Public Enum ExampleKind
40
FirstKind
41
SecondKind
42
ThirdKind
43
FourthKind
44
End Enum
45
46
' An example attribute. The attribute can be applied to all
47
' targets, from assemblies to parameters.
48
'
49
<AttributeUsage(AttributeTargets.All)> _
50
Public Class ExampleAttribute
51
Inherits Attribute
52
53
' Data for properties.
54
Private kindValue As ExampleKind
55
Private noteValue As String
56
Private arrayStrings() As String
57
Private arrayNumbers() As Integer
58
59
' Constructors. The parameterless constructor (.ctor) calls
60
' the constructor that specifies ExampleKind and an array of
61
' strings, and supplies the default values.
62
'
63
Public Sub New(ByVal initKind As ExampleKind, ByVal initStrings() As String)
64
kindValue = initKind
65
arrayStrings = initStrings
66
End Sub
67
Public Sub New(ByVal initKind As ExampleKind)
68
Me.New(initKind, Nothing)
69
End Sub
70
Public Sub New()
71
Me.New(ExampleKind.FirstKind, Nothing)
72
End Sub
73
74
' Properties. The Note and Numbers properties must be read/write, so they
75
' can be used as named parameters.
76
'
77
Public ReadOnly Property Kind As ExampleKind
78
Get
79
Return kindValue
80
End Get
81
End Property
82
Public ReadOnly Property Strings As String()
83
Get
84
Return arrayStrings
85
End Get
86
End Property
87
Public Property Note As String
88
Get
89
Return noteValue
90
End Get
91
Set
92
noteValue = value
93
End Set
94
End Property
95
Public Property Numbers As Integer()
96
Get
97
Return arrayNumbers
98
End Get
99
Set
100
arrayNumbers = value
101
End Set
102
End Property
103
End Class
104
105
' The example attribute is applied to the test class.
106
'
107
<Example(ExampleKind.SecondKind, _
108
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108