Skip to content

Private protected methods are not intercepted #535

@CrispyDrone

Description

@CrispyDrone

Hello,

it seems that private protected methods are not intercepted, even though I would expect them to be:

using System.Runtime.CompilerServices;
using Castle.Core.Internal;
using System;
using NUnit.Framework;
[assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]

namespace Castle.DynamicProxy.Tests
{
	[TestFixture]
	public class BasicClassProxyTestCase : BasePEVerifyTestCase
	{
		private protected virtual void PrivateProtectedThrowException() => throw new Exception("I was called");
		protected virtual void ProtectedThrowException() => throw new Exception("I was called");
		internal virtual void InternalThrowException() => throw new Exception("I was called");

		[Test]
		public void PrivateProtectedVirtualMethodsAreInterceptedCorrectly()
		{
			var proxy = generator.CreateClassProxy<BasicClassProxyTestCase>(
				new DoNothingAndReturnDefaultInterceptor()
			);

			Assert.DoesNotThrow(() => proxy.InternalThrowException());
			Assert.DoesNotThrow(() => proxy.ProtectedThrowException());
			Assert.DoesNotThrow(() => proxy.PrivateProtectedThrowException());
		}

		public class DoNothingAndReturnDefaultInterceptor : StandardInterceptor
		{
			protected override void PerformProceed(IInvocation invocation)
			{
				object returnValue = invocation.ReturnValue;

				if (returnValue != null)
				{
					var returnValueType = returnValue.GetType();

					if (returnValueType.IsValueType)
					{
						invocation.ReturnValue = Activator.CreateInstance(returnValueType);
					}
					else
					{
						invocation.ReturnValue = null;
					}
				}
			}
		}
	}
}

This fails on the private protected call, but not on the internal, or protected one.

Is this expected behavior? If not, there is an existing issue in NSubstitute regarding this, and I think the root cause might then be with dynamicproxy.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions