@@ -16,6 +16,8 @@ namespace NHibernate.Proxy
16
16
{
17
17
internal static class ProxyBuilderHelper
18
18
{
19
+ private static readonly ConstructorInfo ObjectConstructor = typeof ( object ) . GetConstructor ( System . Type . EmptyTypes ) ;
20
+
19
21
#if NETFX
20
22
private static bool _saveAssembly ;
21
23
private static string _saveAssemblyPath ;
@@ -64,6 +66,22 @@ internal static void Save(AssemblyBuilder assemblyBuilder)
64
66
#endif
65
67
}
66
68
69
+ internal static void CallDefaultBaseConstructor ( ILGenerator il , System . Type parentType )
70
+ {
71
+ var baseConstructor = parentType . GetConstructor (
72
+ BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public ,
73
+ null ,
74
+ System . Type . EmptyTypes ,
75
+ null ) ;
76
+ // if there is no default constructor, or the default constructor is private/internal, call System.Object constructor
77
+ // this works, but the generated assembly will fail PeVerify (cannot use in medium trust for example)
78
+ if ( baseConstructor == null || baseConstructor . IsPrivate || baseConstructor . IsAssembly )
79
+ baseConstructor = ObjectConstructor ;
80
+
81
+ il . Emit ( OpCodes . Ldarg_0 ) ;
82
+ il . Emit ( OpCodes . Call , baseConstructor ) ;
83
+ }
84
+
67
85
internal static IEnumerable < MethodInfo > GetProxiableMethods ( System . Type type , IEnumerable < System . Type > interfaces )
68
86
{
69
87
const BindingFlags candidateMethodsBindingFlags =
@@ -83,23 +101,23 @@ internal static MethodBuilder GenerateMethodSignature(string name, MethodInfo me
83
101
if ( method . IsSpecialName )
84
102
methodAttributes |= MethodAttributes . SpecialName ;
85
103
86
- ParameterInfo [ ] parameters = method . GetParameters ( ) ;
104
+ var parameters = method . GetParameters ( ) ;
87
105
88
- MethodBuilder methodBuilder = typeBuilder . DefineMethod (
106
+ var methodBuilder = typeBuilder . DefineMethod (
89
107
name ,
90
108
methodAttributes ,
91
109
CallingConventions . HasThis ,
92
110
method . ReturnType ,
93
111
parameters . Select ( param => param . ParameterType ) . ToArray ( ) ) ;
94
112
95
- System . Type [ ] typeArgs = method . GetGenericArguments ( ) ;
113
+ var typeArgs = method . GetGenericArguments ( ) ;
96
114
97
115
if ( typeArgs . Length > 0 )
98
116
{
99
117
var typeNames = GenerateTypeNames ( typeArgs . Length ) ;
100
118
var typeArgBuilders = methodBuilder . DefineGenericParameters ( typeNames ) ;
101
119
102
- for ( int index = 0 ; index < typeArgs . Length ; index ++ )
120
+ for ( var index = 0 ; index < typeArgs . Length ; index ++ )
103
121
{
104
122
// Copy generic parameter attributes (Covariant, Contravariant, ReferenceTypeConstraint,
105
123
// NotNullableValueTypeConstraint, DefaultConstructorConstraint).
@@ -150,11 +168,10 @@ private static System.Type BuildTypeConstraint(System.Type typeConstraint, Syste
150
168
151
169
var args = new System . Type [ constraintGenericArguments . Length ] ;
152
170
var make = false ;
153
- for ( int index = 0 ; index < constraintGenericArguments . Length ; index ++ )
171
+ for ( var index = 0 ; index < constraintGenericArguments . Length ; index ++ )
154
172
{
155
173
var genericArgument = constraintGenericArguments [ index ] ;
156
- System . Type result ;
157
- if ( parametersMap . TryGetValue ( genericArgument , out result ) )
174
+ if ( parametersMap . TryGetValue ( genericArgument , out var result ) )
158
175
{
159
176
make = true ;
160
177
}
@@ -177,9 +194,9 @@ private static System.Type BuildTypeConstraint(System.Type typeConstraint, Syste
177
194
private static string [ ] GenerateTypeNames ( int count )
178
195
{
179
196
var result = new string [ count ] ;
180
- for ( int index = 0 ; index < count ; index ++ )
197
+ for ( var index = 0 ; index < count ; index ++ )
181
198
{
182
- result [ index ] = string . Format ( "T{0}" , index ) ;
199
+ result [ index ] = $ "T{ index } " ;
183
200
}
184
201
185
202
return result ;
0 commit comments