Skip to content

Commit 587be55

Browse files
Have static proxy not always failing PeVerify
1 parent 047bcef commit 587be55

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/NHibernate/Proxy/NHibernateProxyBuilder.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public TypeInfo CreateProxyType(System.Type baseType, IReadOnlyCollection<System
9696
var customAttributeBuilder = new CustomAttributeBuilder(serializableConstructor, Array.Empty<object>());
9797
typeBuilder.SetCustomAttribute(customAttributeBuilder);
9898

99-
ImplementDeserializationConstructor(typeBuilder);
99+
ImplementDeserializationConstructor(typeBuilder, parentType);
100100
ImplementGetObjectData(typeBuilder, proxyInfoField, lazyInitializerField);
101101

102102
var proxyType = typeBuilder.CreateTypeInfo();
@@ -169,13 +169,24 @@ private static void ImplementConstructor(TypeBuilder typeBuilder, System.Type pa
169169
IL.Emit(OpCodes.Ret);
170170
}
171171

172-
private static void ImplementDeserializationConstructor(TypeBuilder typeBuilder)
172+
private static void ImplementDeserializationConstructor(TypeBuilder typeBuilder, System.Type parentType)
173173
{
174174
var parameterTypes = new[] {typeof (SerializationInfo), typeof (StreamingContext)};
175175
var constructor = typeBuilder.DefineConstructor(constructorAttributes, CallingConventions.Standard, parameterTypes);
176176
constructor.SetImplementationFlags(MethodImplAttributes.IL | MethodImplAttributes.Managed);
177177

178178
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+
179190
//Everything is done in NHibernateProxyObjectReference, so just return data.
180191
IL.Emit(OpCodes.Ret);
181192
}

0 commit comments

Comments
 (0)