-
Notifications
You must be signed in to change notification settings - Fork 466
/
MembersCollector.cs
218 lines (184 loc) · 5.89 KB
/
MembersCollector.cs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Castle.DynamicProxy.Contributors
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Castle.Core.Logging;
using Castle.DynamicProxy.Generators;
using Castle.DynamicProxy.Internal;
internal abstract class MembersCollector
{
private const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
private ILogger logger = NullLogger.Instance;
protected readonly Type type;
protected MembersCollector(Type type)
{
this.type = type;
}
public ILogger Logger
{
get { return logger; }
set { logger = value; }
}
public virtual void CollectMembersToProxy(IProxyGenerationHook hook, IMembersCollectorSink sink)
{
var checkedMethods = new HashSet<MethodInfo>();
CollectProperties();
CollectEvents();
// Methods go last, because properties and events have methods too (getters/setters add/remove)
// and we don't want to get duplicates, so we collect property and event methods first
// then we collect methods, and add only these that aren't there yet
CollectMethods();
void CollectProperties()
{
var propertiesFound = type.GetProperties(Flags);
foreach (var property in propertiesFound)
{
AddProperty(property);
}
}
void CollectEvents()
{
var eventsFound = type.GetEvents(Flags);
foreach (var @event in eventsFound)
{
AddEvent(@event);
}
}
void CollectMethods()
{
var methodsFound = type.GetAllInstanceMethods();
foreach (var method in methodsFound)
{
AddMethod(method, true);
}
}
void AddProperty(PropertyInfo property)
{
MetaMethod getter = null;
MetaMethod setter = null;
if (property.CanRead)
{
var getMethod = property.GetGetMethod(true);
getter = AddMethod(getMethod, false);
}
if (property.CanWrite)
{
var setMethod = property.GetSetMethod(true);
setter = AddMethod(setMethod, false);
}
if (setter == null && getter == null)
{
return;
}
var nonInheritableAttributes = property.GetNonInheritableAttributes();
var arguments = property.GetIndexParameters();
sink.Add(new MetaProperty(property,
getter,
setter,
nonInheritableAttributes.Select(a => a.Builder),
arguments.Select(a => a.ParameterType).ToArray()));
}
void AddEvent(EventInfo @event)
{
var addMethod = @event.GetAddMethod(true);
var removeMethod = @event.GetRemoveMethod(true);
MetaMethod adder = null;
MetaMethod remover = null;
if (addMethod != null)
{
adder = AddMethod(addMethod, false);
}
if (removeMethod != null)
{
remover = AddMethod(removeMethod, false);
}
if (adder == null && remover == null)
{
return;
}
sink.Add(new MetaEvent(@event, adder, remover, EventAttributes.None));
}
MetaMethod AddMethod(MethodInfo method, bool isStandalone)
{
if (checkedMethods.Add(method) == false)
{
return null;
}
if (ProxyUtil.IsAccessibleMethod(method) == false)
{
return null;
}
var methodToGenerate = GetMethodToGenerate(method, hook, isStandalone);
if (methodToGenerate != null)
{
sink.Add(methodToGenerate);
}
return methodToGenerate;
}
}
protected abstract MetaMethod GetMethodToGenerate(MethodInfo method, IProxyGenerationHook hook, bool isStandalone);
/// <summary>
/// Performs some basic screening and invokes the <see cref = "IProxyGenerationHook" />
/// to select methods.
/// </summary>
protected bool AcceptMethod(MethodInfo method, bool onlyVirtuals, IProxyGenerationHook hook)
{
return AcceptMethodPreScreen(method, onlyVirtuals, hook) && hook.ShouldInterceptMethod(type, method);
}
/// <summary>
/// Performs some basic screening to filter out non-interceptable methods.
/// </summary>
/// <remarks>
/// The <paramref name="hook"/> will get invoked for non-interceptable method notification only;
/// it does not get asked whether or not to intercept the <paramref name="method"/>.
/// </remarks>
protected bool AcceptMethodPreScreen(MethodInfo method, bool onlyVirtuals, IProxyGenerationHook hook)
{
// NOTE: at this point, the method's accessibility should already have been checked (see `AddMethod` above)
var isOverridable = method.IsVirtual && !method.IsFinal;
if (onlyVirtuals && !isOverridable)
{
if (method.DeclaringType != typeof(MarshalByRefObject) &&
method.IsGetType() == false &&
method.IsMemberwiseClone() == false)
{
Logger.DebugFormat("Excluded non-overridable method {0} on {1} because it cannot be intercepted.", method.Name,
method.DeclaringType.FullName);
hook.NonProxyableMemberNotification(type, method);
}
return false;
}
// we can never intercept a sealed (final) method
if (method.IsFinal)
{
Logger.DebugFormat("Excluded sealed method {0} on {1} because it cannot be intercepted.", method.Name,
method.DeclaringType.FullName);
return false;
}
if (method.DeclaringType == typeof(MarshalByRefObject))
{
return false;
}
if (method.IsFinalizer())
{
return false;
}
return true;
}
}
}