forked from castleproject/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelegateTypeGenerator.cs
109 lines (97 loc) · 4.19 KB
/
DelegateTypeGenerator.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
// 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.Generators
{
using System;
using System.Reflection;
using Castle.DynamicProxy.Generators.Emitters;
using Castle.DynamicProxy.Generators.Emitters.SimpleAST;
using Castle.DynamicProxy.Internal;
internal class DelegateTypeGenerator : IGenerator<AbstractTypeEmitter>
{
private const TypeAttributes DelegateFlags = TypeAttributes.Class |
TypeAttributes.Public |
TypeAttributes.Sealed |
TypeAttributes.AnsiClass |
TypeAttributes.AutoClass;
private readonly MetaMethod method;
private readonly Type targetType;
public DelegateTypeGenerator(MetaMethod method, Type targetType)
{
this.method = method;
this.targetType = targetType;
}
public AbstractTypeEmitter Generate(ClassEmitter @class, INamingScope namingScope)
{
var emitter = GetEmitter(@class, namingScope);
BuildConstructor(emitter);
BuildInvokeMethod(emitter);
return emitter;
}
private void BuildConstructor(AbstractTypeEmitter emitter)
{
var constructor = emitter.CreateConstructor(new ArgumentReference(typeof(object)),
new ArgumentReference(typeof(IntPtr)));
constructor.ConstructorBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
}
private void BuildInvokeMethod(AbstractTypeEmitter @delegate)
{
var paramTypes = GetParamTypes(@delegate);
var invoke = @delegate.CreateMethod("Invoke",
MethodAttributes.Public |
MethodAttributes.HideBySig |
MethodAttributes.NewSlot |
MethodAttributes.Virtual,
@delegate.GetClosedParameterType(method.MethodOnTarget.ReturnType),
paramTypes);
invoke.MethodBuilder.SetImplementationFlags(MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
}
private AbstractTypeEmitter GetEmitter(ClassEmitter @class, INamingScope namingScope)
{
var methodInfo = method.MethodOnTarget;
var suggestedName = string.Format("Castle.Proxies.Delegates.{0}_{1}",
methodInfo.DeclaringType.Name,
method.Method.Name);
var uniqueName = namingScope.ParentScope.GetUniqueName(suggestedName);
var @delegate = new ClassEmitter(@class.ModuleScope,
uniqueName,
typeof(MulticastDelegate),
Type.EmptyTypes,
DelegateFlags,
forceUnsigned: @class.InStrongNamedModule == false);
@delegate.CopyGenericParametersFromMethod(method.Method);
return @delegate;
}
private Type[] GetParamTypes(AbstractTypeEmitter @delegate)
{
var parameters = method.MethodOnTarget.GetParameters();
if (@delegate.TypeBuilder.IsGenericType)
{
var types = new Type[parameters.Length];
for (var i = 0; i < parameters.Length; i++)
{
types[i] = @delegate.GetClosedParameterType(parameters[i].ParameterType);
}
return types;
}
var paramTypes = new Type[parameters.Length + 1];
paramTypes[0] = targetType;
for (var i = 0; i < parameters.Length; i++)
{
paramTypes[i + 1] = @delegate.GetClosedParameterType(parameters[i].ParameterType);
}
return paramTypes;
}
}
}