blob: b91a0c52a30f9c12aa594ce075955f011205c50f (
plain)
1
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
109
110
111
112
113
114
|
#if !SILVERLIGHT && CLR40
using System;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
namespace Microsoft.Internal
{
internal static class ReflectionInvoke
{
private static readonly ReflectionPermission _memberAccess = new ReflectionPermission(ReflectionPermissionFlag.MemberAccess);
private static readonly ReflectionPermission _restrictedMemberAccess = new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess);
public static object SafeCreateInstance(this Type type, params object[] arguments)
{
DemandMemberAccessIfNeeded(type);
return Activator.CreateInstance(type, arguments);
}
public static object SafeInvoke(this ConstructorInfo constructor, params object[] arguments)
{
DemandMemberAccessIfNeeded(constructor);
return constructor.Invoke(arguments);
}
public static object SafeInvoke(this MethodInfo method, object instance, params object[] arguments)
{
DemandMemberAccessIfNeeded(method);
return method.Invoke(instance, arguments);
}
public static object SafeGetValue(this FieldInfo field, object instance)
{
DemandMemberAccessIfNeeded(field);
return field.GetValue(instance);
}
public static void SafeSetValue(this FieldInfo field, object instance, object value)
{
DemandMemberAccessIfNeeded(field);
field.SetValue(instance, value);
}
public static void DemandMemberAccessIfNeeded(MethodInfo method)
{
if (!method.IsVisible())
{
DemandMemberAccess(method);
}
}
private static void DemandMemberAccessIfNeeded(FieldInfo field)
{
if (!field.IsVisible())
{
DemandMemberAccess(field);
}
}
public static void DemandMemberAccessIfNeeded(Type type)
{
// Consult UnderlyingSystemType this is the type that Activator.CreateInstance creates
if (!type.UnderlyingSystemType.IsVisible)
{
DemandMemberAccess(type);
}
}
private static void DemandMemberAccessIfNeeded(ConstructorInfo constructor)
{
if (!constructor.IsVisible())
{
DemandMemberAccess(constructor);
}
}
private static void DemandMemberAccess(MemberInfo target)
{
try
{
_memberAccess.Demand();
}
catch (SecurityException)
{ // The caller doesn't have member access, but let's see whether they have access to
// members of assemblies with less or equal permissions (this mimics Reflection's behavior)
DemandRestrictedMemberAccess(target);
}
}
private static void DemandRestrictedMemberAccess(MemberInfo target)
{
Assembly targetAssembly = target.Assembly();
PermissionSet targetGrantSet = UnsafePermissionSet(targetAssembly);
targetGrantSet.AddPermission(_restrictedMemberAccess);
targetGrantSet.Demand();
}
[SecuritySafeCritical] // PermissionSet is [SecurityCritical]
private static PermissionSet UnsafePermissionSet(Assembly assembly)
{
return assembly.PermissionSet;
}
}
}
#endif
|