-
Notifications
You must be signed in to change notification settings - Fork 466
/
MembersCollector.cs
250 lines (212 loc) · 7.24 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2004-2011 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;
private ICollection<MethodInfo> checkedMethods = new HashSet<MethodInfo>();
private readonly IDictionary<PropertyInfo, MetaProperty> properties = new Dictionary<PropertyInfo, MetaProperty>();
private readonly IDictionary<EventInfo, MetaEvent> events = new Dictionary<EventInfo, MetaEvent>();
private readonly IDictionary<MethodInfo, MetaMethod> methods = new Dictionary<MethodInfo, MetaMethod>();
protected readonly Type type;
protected MembersCollector(Type type)
{
this.type = type;
}
public ILogger Logger
{
get { return logger; }
set { logger = value; }
}
public IEnumerable<MetaMethod> Methods
{
get { return methods.Values; }
}
public IEnumerable<MetaProperty> Properties
{
get { return properties.Values; }
}
public IEnumerable<MetaEvent> Events
{
get { return events.Values; }
}
public virtual void CollectMembersToProxy(IProxyGenerationHook hook)
{
if (checkedMethods == null) // this method was already called!
{
throw new InvalidOperationException(
string.Format("Can't call 'CollectMembersToProxy' method twice. This usually signifies a bug in custom {0}.",
typeof(ITypeContributor)));
}
CollectProperties(hook);
CollectEvents(hook);
// 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(hook);
checkedMethods = null; // this is ugly, should have a boolean flag for this or something
}
private void CollectProperties(IProxyGenerationHook hook)
{
var propertiesFound = type.GetProperties(Flags);
foreach (var property in propertiesFound)
{
AddProperty(property, hook);
}
}
private void CollectEvents(IProxyGenerationHook hook)
{
var eventsFound = type.GetEvents(Flags);
foreach (var @event in eventsFound)
{
AddEvent(@event, hook);
}
}
private void CollectMethods(IProxyGenerationHook hook)
{
var methodsFound = MethodFinder.GetAllInstanceMethods(type, Flags);
foreach (var method in methodsFound)
{
AddMethod(method, hook, true);
}
}
private void AddProperty(PropertyInfo property, IProxyGenerationHook hook)
{
MetaMethod getter = null;
MetaMethod setter = null;
if (property.CanRead)
{
var getMethod = property.GetGetMethod(true);
getter = AddMethod(getMethod, hook, false);
}
if (property.CanWrite)
{
var setMethod = property.GetSetMethod(true);
setter = AddMethod(setMethod, hook, false);
}
if (setter == null && getter == null)
{
return;
}
var nonInheritableAttributes = property.GetNonInheritableAttributes();
var arguments = property.GetIndexParameters();
properties[property] = new MetaProperty(property.Name,
property.PropertyType,
property.DeclaringType,
getter,
setter,
nonInheritableAttributes.Select(a => a.Builder),
arguments.Select(a => a.ParameterType).ToArray());
}
private void AddEvent(EventInfo @event, IProxyGenerationHook hook)
{
var addMethod = @event.GetAddMethod(true);
var removeMethod = @event.GetRemoveMethod(true);
MetaMethod adder = null;
MetaMethod remover = null;
if (addMethod != null)
{
adder = AddMethod(addMethod, hook, false);
}
if (removeMethod != null)
{
remover = AddMethod(removeMethod, hook, false);
}
if (adder == null && remover == null)
{
return;
}
events[@event] = new MetaEvent(@event.Name,
@event.DeclaringType, @event.EventHandlerType, adder, remover, EventAttributes.None);
}
private MetaMethod AddMethod(MethodInfo method, IProxyGenerationHook hook, bool isStandalone)
{
if (checkedMethods.Contains(method))
{
return null;
}
checkedMethods.Add(method);
if (methods.ContainsKey(method))
{
return null;
}
var methodToGenerate = GetMethodToGenerate(method, hook, isStandalone);
if (methodToGenerate != null)
{
methods[method] = 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)
{
if (IsInternalAndNotVisibleToDynamicProxy(method))
{
return false;
}
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;
}
//can only proxy methods that are public or protected (or internals that have already been checked above)
if ((method.IsPublic || method.IsFamily || method.IsAssembly || method.IsFamilyOrAssembly) == false)
{
return false;
}
if (method.DeclaringType == typeof(MarshalByRefObject))
{
return false;
}
if (method.IsFinalizer())
{
return false;
}
return hook.ShouldInterceptMethod(type, method);
}
private static bool IsInternalAndNotVisibleToDynamicProxy(MethodInfo method)
{
return ProxyUtil.IsInternal(method) &&
ProxyUtil.AreInternalsVisibleToDynamicProxy(method.DeclaringType.Assembly) == false;
}
}
}