private static void ImplementDeserializationConstructor(TypeBuilder typeBuilder, System.Type parentType)
        {
            var parameterTypes = new[] { typeof(SerializationInfo), typeof(StreamingContext) };
            var constructor    = typeBuilder.DefineConstructor(constructorAttributes, CallingConventions.Standard, parameterTypes);

            constructor.SetImplementationFlags(MethodImplAttributes.IL | MethodImplAttributes.Managed);

            var IL = constructor.GetILGenerator();

            ProxyBuilderHelper.CallDefaultBaseConstructor(IL, parentType);
            //Everything is done in NHibernateProxyObjectReference, so just return data.
            IL.Emit(OpCodes.Ret);
        }
        private static void ImplementConstructor(TypeBuilder typeBuilder, System.Type parentType, FieldInfo proxyInfoField)
        {
            var constructor = typeBuilder.DefineConstructor(constructorAttributes, CallingConventions.Standard, new[] { typeof(NHibernateProxyFactoryInfo) });

            constructor.SetImplementationFlags(MethodImplAttributes.IL | MethodImplAttributes.Managed);

            var IL = constructor.GetILGenerator();

            ProxyBuilderHelper.CallDefaultBaseConstructor(IL, parentType);

            // __proxyInfo == proxyInfo;
            IL.Emit(OpCodes.Ldarg_0);
            IL.Emit(OpCodes.Ldarg_1);
            IL.Emit(OpCodes.Stfld, proxyInfoField);

            IL.Emit(OpCodes.Ret);
        }
        private static void ImplementDeserializationConstructor(TypeBuilder typeBuilder, System.Type parentType)
        {
            var parameterTypes = new[] { typeof(SerializationInfo), typeof(StreamingContext) };
            var constructor    = typeBuilder.DefineConstructor(constructorAttributes, CallingConventions.Standard, parameterTypes);

            constructor.SetImplementationFlags(MethodImplAttributes.IL | MethodImplAttributes.Managed);

            var IL = constructor.GetILGenerator();

            if (typeof(ISerializable).IsAssignableFrom(parentType))
            {
                var baseConstructor = parentType.GetConstructor(
                    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                    null,
                    parameterTypes,
                    null);
                if (baseConstructor == null)
                {
                    // Throw new InvalidOperationException(<message>);
                    IL.Emit(
                        OpCodes.Ldstr,
                        $"Proxy for class {parentType.FullName} cannot be deserialized because the class implements " +
                        $"{nameof(ISerializable)} without having a deserialization constructor (see CA2229).");
                    IL.Emit(OpCodes.Newobj, InvalidOperationWithMessageConstructor);
                    IL.Emit(OpCodes.Throw);
                }
                else
                {
                    IL.Emit(OpCodes.Ldarg_0);
                    IL.Emit(OpCodes.Ldarg_1);
                    IL.Emit(OpCodes.Ldarg_2);
                    IL.Emit(OpCodes.Call, baseConstructor);
                }
            }
            else
            {
                ProxyBuilderHelper.CallDefaultBaseConstructor(IL, parentType);
            }

            // Everything else is done in FieldInterceptorObjectReference.
            IL.Emit(OpCodes.Ret);
        }