@@ -96,7 +96,7 @@ public TypeInfo CreateProxyType(System.Type baseType, IReadOnlyCollection<System
96
96
var customAttributeBuilder = new CustomAttributeBuilder ( serializableConstructor , Array . Empty < object > ( ) ) ;
97
97
typeBuilder . SetCustomAttribute ( customAttributeBuilder ) ;
98
98
99
- ImplementDeserializationConstructor ( typeBuilder ) ;
99
+ ImplementDeserializationConstructor ( typeBuilder , parentType ) ;
100
100
ImplementGetObjectData ( typeBuilder , proxyInfoField , lazyInitializerField ) ;
101
101
102
102
var proxyType = typeBuilder . CreateTypeInfo ( ) ;
@@ -169,13 +169,24 @@ private static void ImplementConstructor(TypeBuilder typeBuilder, System.Type pa
169
169
IL . Emit ( OpCodes . Ret ) ;
170
170
}
171
171
172
- private static void ImplementDeserializationConstructor ( TypeBuilder typeBuilder )
172
+ private static void ImplementDeserializationConstructor ( TypeBuilder typeBuilder , System . Type parentType )
173
173
{
174
174
var parameterTypes = new [ ] { typeof ( SerializationInfo ) , typeof ( StreamingContext ) } ;
175
175
var constructor = typeBuilder . DefineConstructor ( constructorAttributes , CallingConventions . Standard , parameterTypes ) ;
176
176
constructor . SetImplementationFlags ( MethodImplAttributes . IL | MethodImplAttributes . Managed ) ;
177
177
178
178
var IL = constructor . GetILGenerator ( ) ;
179
+
180
+ constructor . SetImplementationFlags ( MethodImplAttributes . IL | MethodImplAttributes . Managed ) ;
181
+
182
+ var baseConstructor = parentType . GetConstructor ( BindingFlags . Instance | BindingFlags . NonPublic | BindingFlags . Public , null , System . Type . EmptyTypes , null ) ;
183
+ // if there is no default constructor, or the default constructor is private/internal, call System.Object constructor
184
+ // this works, but the generated assembly will fail PeVerify (cannot use in medium trust for example)
185
+ if ( baseConstructor == null || baseConstructor . IsPrivate || baseConstructor . IsAssembly )
186
+ baseConstructor = ObjectConstructor ;
187
+ IL . Emit ( OpCodes . Ldarg_0 ) ;
188
+ IL . Emit ( OpCodes . Call , baseConstructor ) ;
189
+
179
190
//Everything is done in NHibernateProxyObjectReference, so just return data.
180
191
IL . Emit ( OpCodes . Ret ) ;
181
192
}
0 commit comments