Skip to content

Commit

Permalink
Allows override a protected method in the proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyredondo committed Jun 23, 2020
1 parent debd47e commit 5d3bf14
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/Wanhjor.ObjectInspector/DuckType.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ private static List<MethodInfo> GetMethods(Type baseType)
return selectedMethods;
static IEnumerable<MethodInfo> GetBaseMethods(Type baseType)
{
foreach (var method in baseType.GetMethods())
foreach (var method in baseType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
if (method.IsSpecialName || method.DeclaringType == typeof(DuckType))
if (method.IsSpecialName || method.IsFinal || method.IsPrivate || method.DeclaringType == typeof(DuckType))
continue;
if (baseType.IsInterface || method.IsAbstract || method.IsVirtual)
yield return method;
Expand All @@ -40,6 +40,11 @@ private static void CreateMethods(Type baseType, Type instanceType, FieldInfo in
var iMethodParameters = iMethod.GetParameters();
var iMethodParametersTypes = iMethodParameters.Select(p => p.ParameterType).ToArray();

// We select the method to call
var method = SelectMethod(instanceType, iMethod, iMethodParameters, iMethodParametersTypes);
if (method is null && iMethod.IsVirtual)
continue;

var attributes = iMethod.IsAbstract || iMethod.IsVirtual
? MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.HideBySig
: MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.HideBySig | MethodAttributes.NewSlot;
Expand All @@ -66,14 +71,11 @@ private static void CreateMethods(Type baseType, Type instanceType, FieldInfo in
var il = methodBuilder.GetILGenerator();
var publicInstance = instanceType.IsPublic || instanceType.IsNestedPublic;

// We select the method to call
var method = SelectMethod(instanceType, iMethod, iMethodParameters, iMethodParametersTypes);

if (method is null)
{
il.Emit(OpCodes.Newobj, typeof(NotImplementedException).GetConstructor(Type.EmptyTypes)!);
il.Emit(OpCodes.Throw);
return;
continue;
}

var innerDuck = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Wanhjor.ObjectInspector/Wanhjor.ObjectInspector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Description>An efficient .NET object inspector/accesor to avoid reflection usage with duck typing support.</Description>
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
<Version>0.4.0-beta.3</Version>
<Version>0.4.0-beta.4</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>ObjectInspector</Title>
<Authors>Daniel Redondo</Authors>
Expand Down
24 changes: 23 additions & 1 deletion test/Wanhjor.ObjectInspector.Tests/DuckTypeAbstractTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public void ReverseDuckTest()

var name = duck.GetName();
Assert.Equal("From Base", name);

var value = duck.GetValue();
}
}

Expand All @@ -151,7 +153,17 @@ public abstract class AbstractDuckTestNoInherits
public abstract string Name { get; set; }
}

public abstract class ReverseDuck
public abstract class ReverseDuckBase : IDisposable
{
public abstract string GetValue();

public void Dispose()
{

}
}

public abstract class ReverseDuck : ReverseDuckBase
{
public string Name { get; set; } = "From Base";

Expand All @@ -162,6 +174,11 @@ public string GetValues()
return $"{Name} => {Value}";
}

public override string GetValue()
{
return string.Empty;
}

public abstract string GetName();
}

Expand All @@ -179,6 +196,11 @@ public string GetName()
{
return _extractor.Name;
}
/*
public string GetValue()
{
return Value;
}*/

public void SetProxyObject(object proxy)
{
Expand Down

0 comments on commit 5d3bf14

Please sign in to comment.