/
CompositeTypeContributor.cs
149 lines (129 loc) · 4.47 KB
/
CompositeTypeContributor.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
// 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.Diagnostics;
using System.Reflection;
using Castle.Core.Logging;
using Castle.DynamicProxy.Generators;
using Castle.DynamicProxy.Generators.Emitters;
using Castle.DynamicProxy.Internal;
internal abstract class CompositeTypeContributor : ITypeContributor
{
protected readonly INamingScope namingScope;
protected readonly ICollection<Type> interfaces = new HashSet<Type>();
private ILogger logger = NullLogger.Instance;
private readonly ICollection<MetaProperty> properties = new TypeElementCollection<MetaProperty>();
private readonly ICollection<MetaEvent> events = new TypeElementCollection<MetaEvent>();
private readonly ICollection<MetaMethod> methods = new TypeElementCollection<MetaMethod>();
protected CompositeTypeContributor(INamingScope namingScope)
{
this.namingScope = namingScope;
}
public ILogger Logger
{
get { return logger; }
set { logger = value; }
}
public void CollectElementsToProxy(IProxyGenerationHook hook, MetaType model)
{
foreach (var collector in CollectElementsToProxyInternal(hook))
{
foreach (var method in collector.Methods)
{
model.AddMethod(method);
methods.Add(method);
}
foreach (var @event in collector.Events)
{
model.AddEvent(@event);
events.Add(@event);
}
foreach (var property in collector.Properties)
{
model.AddProperty(property);
properties.Add(property);
}
}
}
protected abstract IEnumerable<MembersCollector> CollectElementsToProxyInternal(IProxyGenerationHook hook);
public virtual void Generate(ClassEmitter @class)
{
foreach (var method in methods)
{
if (!method.Standalone)
{
continue;
}
ImplementMethod(method,
@class,
@class.CreateMethod);
}
foreach (var property in properties)
{
ImplementProperty(@class, property);
}
foreach (var @event in events)
{
ImplementEvent(@class, @event);
}
}
public void AddInterfaceToProxy(Type @interface)
{
Debug.Assert(@interface != null, "@interface == null", "Shouldn't be adding empty interfaces...");
Debug.Assert(@interface.IsInterface || @interface.IsDelegateType(), "@interface.IsInterface || @interface.IsDelegateType()", "Should be adding interfaces or delegate types only...");
Debug.Assert(!interfaces.Contains(@interface), "!interfaces.ContainsKey(@interface)",
"Shouldn't be adding same interface twice...");
interfaces.Add(@interface);
}
private void ImplementEvent(ClassEmitter emitter, MetaEvent @event)
{
@event.BuildEventEmitter(emitter);
ImplementMethod(@event.Adder, emitter, @event.Emitter.CreateAddMethod);
ImplementMethod(@event.Remover, emitter, @event.Emitter.CreateRemoveMethod);
}
private void ImplementProperty(ClassEmitter emitter, MetaProperty property)
{
property.BuildPropertyEmitter(emitter);
if (property.CanRead)
{
ImplementMethod(property.Getter, emitter, property.Emitter.CreateGetMethod);
}
if (property.CanWrite)
{
ImplementMethod(property.Setter, emitter, property.Emitter.CreateSetMethod);
}
}
protected abstract MethodGenerator GetMethodGenerator(MetaMethod method, ClassEmitter @class,
OverrideMethodDelegate overrideMethod);
private void ImplementMethod(MetaMethod method, ClassEmitter @class,
OverrideMethodDelegate overrideMethod)
{
{
var generator = GetMethodGenerator(method, @class, overrideMethod);
if (generator == null)
{
return;
}
var proxyMethod = generator.Generate(@class, namingScope);
foreach (var attribute in method.Method.GetNonInheritableAttributes())
{
proxyMethod.DefineCustomAttribute(attribute.Builder);
}
}
}
}
}