Class TemplateMethodQuery

Namespace: AutoFixture.Kernel
Assembly: AutoFixture.dll

Selects public static methods that has the same parameters of another method, ignoring optional parameters and return type.

public class TemplateMethodQuery : IMethodQuery

Inheritance

objectTemplateMethodQuery

Implements

IMethodQuery

Inherited Members

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Remarks

The main target of this `IMethodQuery` implementation is to be able to late bind to a method even if it has optional parameters added to it.

The order of the methods are based on the match of parameters types of both methods, favoring the method with exactly same parameters to be returned first.

Constructors

TemplateMethodQuery(MethodInfo)

Initializes a new instance of the TemplateMethodQuery class.

public TemplateMethodQuery(MethodInfo template)

Parameters

template MethodInfo

The method info to compare.

TemplateMethodQuery(MethodInfo, object)

Initializes a new instance of the TemplateMethodQuery class.

public TemplateMethodQuery(MethodInfo template, object owner)

Parameters

template MethodInfo

The method info to compare.

owner object

The owner.

Properties

Owner

The owner instance of the MethodInfo.

public object Owner { get; }

Property Value

object

Template

The template MethodInfo to compare.

public MethodInfo Template { get; }

Property Value

MethodInfo

Methods

SelectMethods(Type)

Selects the methods for the supplied type similar to Template.

public IEnumerable<IMethod> SelectMethods(Type type)

Parameters

type Type

The type.

Returns

IEnumerable<IMethod>

All public static methods for type, ordered by the most similar first.

Remarks

The ordering of the returned methods is based on a score that matches the parameters types of the method with the `Template` method parameters. Methods with the highest score are returned before.

The score is calculated with the following rules: The methods earns 100 points for each exact match parameter type, it loses 50 points for each hierarchy level down of non-matching parameter type and it loses 1 point for each optional parameter. It also sums the score for the generic type arguments or element type with a 10% weight and will decrease 50 points for the difference between the length of the type arguments array. It also gives 5 points if the type is a class instead of an interface.

In case of two methods with an equal score, the ordering is unspecified.