Skip to content

Add support for delegate type values through StringArgumentValue #259 #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -71,6 +71,43 @@ public object ConvertTo(Type toType, ResolutionContext resolutionContext)
TryParseStaticMemberAccessor(argumentValue, out var accessorTypeName, out var memberName))
{
var accessorType = Type.GetType(accessorTypeName, throwOnError: true);

// if delegate, look for a method and then construct a delegate
if (typeof(Delegate).IsAssignableFrom(toType) || typeof(MethodInfo) == toType)
{
var methodCandidates = accessorType.GetTypeInfo().DeclaredMethods
.Where(x => x.Name == memberName)
.Where(x => x.IsPublic)
.Where(x => !x.IsGenericMethod)
.Where(x => x.IsStatic)
.ToList();

if (methodCandidates.Count > 1 && typeof(Delegate).IsAssignableFrom(toType))
{
// filter possible method overloads

var delegateSig = toType.GetMethod("Invoke");
var delegateParameters = delegateSig!.GetParameters().Select(x => x.ParameterType);
methodCandidates = methodCandidates
.Where(x => x.ReturnType == delegateSig.ReturnType && x.GetParameters().Select(y => y.ParameterType).SequenceEqual(delegateParameters))
.ToList();
}

var methodCandidate = methodCandidates.SingleOrDefault();

if (methodCandidate != null)
{
if (typeof(MethodInfo) == toType)
{
return methodCandidate;
}
else
{
return methodCandidate.CreateDelegate(toType);
}
}
}

// is there a public static property with that name ?
var publicStaticPropertyInfo = accessorType.GetTypeInfo().DeclaredProperties
.Where(x => x.Name == memberName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ public void StaticMembersAccessorsCanBeUsedForAbstractTypes(string input, Type t
Assert.Equal(ConcreteImpl.Instance, actual);
}

[Theory]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::FuncIntParseField, Serilog.Settings.Configuration.Tests", typeof(Func<string, int>))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::NamedIntParseField, Serilog.Settings.Configuration.Tests", typeof(NamedIntParse))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::FuncIntParseProperty, Serilog.Settings.Configuration.Tests", typeof(Func<string, int>))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::NamedIntParseProperty, Serilog.Settings.Configuration.Tests", typeof(NamedIntParse))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::IntParseMethod, Serilog.Settings.Configuration.Tests", typeof(NamedIntParse))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::IntParseMethod, Serilog.Settings.Configuration.Tests", typeof(Func<string, int>))]
public void StaticMembersAccessorsCanBeUsedForDelegateTypes(string input, Type targetType)
{
var stringArgumentValue = new StringArgumentValue(input);

var actual = stringArgumentValue.ConvertTo(targetType, new ResolutionContext());

Assert.IsAssignableFrom(targetType, actual);
}

[Theory]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::ConcreteClassProperty, Serilog.Settings.Configuration.Tests", typeof(AConcreteClass))]
[InlineData("Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors::ConcreteClassField, Serilog.Settings.Configuration.Tests", typeof(AConcreteClass))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
namespace Serilog.Settings.Configuration.Tests.Support
using System;

namespace Serilog.Settings.Configuration.Tests.Support
{
public delegate int NamedIntParse(string value);

public interface IAmAnInterface
{
}
Expand Down Expand Up @@ -46,5 +50,13 @@ public class ClassWithStaticAccessors
#pragma warning restore 169
public IAmAnInterface InstanceInterfaceProperty => ConcreteImpl.Instance;
public IAmAnInterface InstanceInterfaceField = ConcreteImpl.Instance;

public static Func<string, int> FuncIntParseField = int.Parse;
public static NamedIntParse NamedIntParseField = int.Parse;
public static Func<string, int> FuncIntParseProperty => int.Parse;
public static NamedIntParse NamedIntParseProperty => int.Parse;
public static int IntParseMethod(string value) => int.Parse(value);
public static int IntParseMethod(string value, string otherValue) => int.Parse(value); // will not be chosen, extra parameter
public static int IntParseMethod(object value) => throw new NotImplementedException(); // will not be chosen, wrong parameter type
}
}