Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,21 @@ public void ClassProxyShouldCallProtectedDefaultConstructor()
Assert.AreEqual("Something", ((ClassWithProtectedDefaultConstructor)proxy2).SomeString);
}

[Test]
public void ClassProxyShouldHaveDefaultConstructorWhenBaseClassHasPrivateProtected()
{
object proxy = generator.CreateClassProxy<ClassWithPrivateProtectedConstructor>();
Assert.IsNotNull(Activator.CreateInstance(proxy.GetType()));
}

[Test]
public void ClassProxyShouldCallPrivateProtectedDefaultConstructor()
{
object proxy = generator.CreateClassProxy<ClassWithPrivateProtectedConstructor>();
object proxy2 = Activator.CreateInstance(proxy.GetType());
Assert.AreEqual("Something", ((ClassWithPrivateProtectedConstructor)proxy2).SomeString);
}

[Test]
public void ClassImplementingInterfaceVitrually()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2004-2020 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.DynamicProxy.Tests.Classes
{
public class ClassWithPrivateProtectedConstructor
{
private protected ClassWithPrivateProtectedConstructor()
{
_someString = "Something";
}

private string _someString = string.Empty;

public string SomeString
{
get { return _someString; }
set { _someString = value; }
}
}
}
10 changes: 1 addition & 9 deletions src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ protected void GenerateConstructors(ClassEmitter emitter, Type baseType, params

foreach (var constructor in constructors)
{
if (!IsConstructorVisible(constructor))
if (!ProxyUtil.IsAccessibleMethod(constructor))
{
continue;
}
Expand Down Expand Up @@ -409,14 +409,6 @@ protected Type ObtainProxyType(CacheKey cacheKey, Func<string, INamingScope, Typ
return proxyType;
}

private bool IsConstructorVisible(ConstructorInfo constructor)
{
return constructor.IsPublic ||
constructor.IsFamily ||
constructor.IsFamilyOrAssembly ||
(constructor.IsAssembly && ProxyUtil.AreInternalsVisibleToDynamicProxy(constructor.DeclaringType.GetTypeInfo().Assembly));
}

private bool OverridesEqualsAndGetHashCode(Type type)
{
var equalsMethod = type.GetMethod("Equals", BindingFlags.Public | BindingFlags.Instance);
Expand Down