Table of Contents

Class ReflectionExtensions

Namespace
Xunit.Sdk

Extension methods for reflection types in .NET.

public static class ReflectionExtensions
Inheritance
ReflectionExtensions
Inherited Members

Methods

GetDefaultValue(Type)

Returns the default value for the given type. For value types, this means a 0-initialized instance of the type; for reference types, this means null.

public static object? GetDefaultValue(this Type type)

Parameters

type Type

The type to get the default value of.

Returns

object

The default value for the given type.

GetDisplayNameWithArguments(MethodInfo, string, object?[]?, Type[]?)

Formulates the extended portion of the display name for a test method. For tests with no arguments, this will return just the base name; for tests with arguments, attempts to format the arguments and appends the argument list to the test name.

public static string GetDisplayNameWithArguments(this MethodInfo method, string baseDisplayName, object?[]? arguments, Type[]? genericTypes)

Parameters

method MethodInfo

The test method

baseDisplayName string

The base part of the display name

arguments object[]

The test method arguments

genericTypes Type[]

The test method's generic types

Returns

string

The full display name for the test method

GetMatchingCustomAttributes(Attribute, string)

Gets all the custom attributes for the attribute that are of the given attribute type.

public static IReadOnlyCollection<Attribute> GetMatchingCustomAttributes(this Attribute attribute, string assemblyQualifiedTypeName)

Parameters

attribute Attribute

The attribute to get custom attributes for.

assemblyQualifiedTypeName string

The type of the attribute to find. Will accept attribute types that are concrete, closed generic, and open generic. When provided an open generic type (e.g., MyAttribute<>) it will return matching closed generic attributes (e.g., MyAttribute>int<)

Returns

IReadOnlyCollection<Attribute>

The matching attributes that decorate the attribute

GetMatchingCustomAttributes(Attribute, Type)

Gets all the custom attributes for the attribute that are of the given attribute type.

public static IReadOnlyCollection<Attribute> GetMatchingCustomAttributes(this Attribute attribute, Type attributeType)

Parameters

attribute Attribute

The attribute to get custom attributes for.

attributeType Type

The type of the attribute to find. Will accept attribute types that are concrete, closed generic, and open generic. When provided an open generic type (e.g., MyAttribute<>) it will return matching closed generic attributes (e.g., MyAttribute>int<)

Returns

IReadOnlyCollection<Attribute>

The matching attributes that decorate the attribute

GetMatchingCustomAttributes(Assembly, string)

Gets all the custom attributes for the assembly that are of the given attribute type.

public static IReadOnlyCollection<Attribute> GetMatchingCustomAttributes(this Assembly assembly, string assemblyQualifiedTypeName)

Parameters

assembly Assembly

The assembly to get custom attributes for.

assemblyQualifiedTypeName string

The type of the attribute to find. Will accept attribute types that are concrete, closed generic, and open generic. When provided an open generic type (e.g., MyAttribute<>) it will return matching closed generic attributes (e.g., MyAttribute>int<)

Returns

IReadOnlyCollection<Attribute>

The matching attributes that decorate the assembly

GetMatchingCustomAttributes(Assembly, Type)

Gets all the custom attributes for the assembly that are of the given attribute type.

public static IReadOnlyCollection<Attribute> GetMatchingCustomAttributes(this Assembly assembly, Type attributeType)

Parameters

assembly Assembly

The assembly to get custom attributes for.

attributeType Type

The type of the attribute to find. Will accept attribute types that are concrete, closed generic, and open generic. When provided an open generic type (e.g., MyAttribute<>) it will return matching closed generic attributes (e.g., MyAttribute>int<)

Returns

IReadOnlyCollection<Attribute>

The matching attributes that decorate the assembly

GetMatchingCustomAttributes(MethodInfo, string)

Gets all the custom attributes for the method that are of the given attribute type.

public static IReadOnlyCollection<Attribute> GetMatchingCustomAttributes(this MethodInfo method, string assemblyQualifiedTypeName)

Parameters

method MethodInfo

The method to get custom attributes for.

assemblyQualifiedTypeName string

The type of the attribute to find. Will accept attribute types that are concrete, closed generic, and open generic. When provided an open generic type (e.g., MyAttribute<>) it will return matching closed generic attributes (e.g., MyAttribute>int<)

Returns

IReadOnlyCollection<Attribute>

The matching attributes that decorate the method

GetMatchingCustomAttributes(MethodInfo, Type)

Gets all the custom attributes for the method that are of the given attribute type.

public static IReadOnlyCollection<Attribute> GetMatchingCustomAttributes(this MethodInfo method, Type attributeType)

Parameters

method MethodInfo

The method to get custom attributes for.

attributeType Type

The type of the attribute to find. Will accept attribute types that are concrete, closed generic, and open generic. When provided an open generic type (e.g., MyAttribute<>) it will return matching closed generic attributes (e.g., MyAttribute>int<)

Returns

IReadOnlyCollection<Attribute>

The matching attributes that decorate the method

GetMatchingCustomAttributes(ParameterInfo, string)

Gets all the custom attributes for the parameter that are of the given attribute type.

public static IReadOnlyCollection<Attribute> GetMatchingCustomAttributes(this ParameterInfo parameter, string assemblyQualifiedTypeName)

Parameters

parameter ParameterInfo

The parameter to get custom attributes for.

assemblyQualifiedTypeName string

The type of the attribute to find. Will accept attribute types that are concrete, closed generic, and open generic. When provided an open generic type (e.g., MyAttribute<>) it will return matching closed generic attributes (e.g., MyAttribute>int<)

Returns

IReadOnlyCollection<Attribute>

The matching attributes that decorate the parameter

GetMatchingCustomAttributes(ParameterInfo, Type)

Gets all the custom attributes for the parameter that are of the given attribute type.

public static IReadOnlyCollection<Attribute> GetMatchingCustomAttributes(this ParameterInfo parameter, Type attributeType)

Parameters

parameter ParameterInfo

The parameter to get custom attributes for.

attributeType Type

The type of the attribute to find. Will accept attribute types that are concrete, closed generic, and open generic. When provided an open generic type (e.g., MyAttribute<>) it will return matching closed generic attributes (e.g., MyAttribute>int<)

Returns

IReadOnlyCollection<Attribute>

The matching attributes that decorate the parameter

GetMatchingCustomAttributes(Type, string)

Gets all the custom attributes for the type that are of the given attribute type.

public static IReadOnlyCollection<Attribute> GetMatchingCustomAttributes(this Type type, string assemblyQualifiedTypeName)

Parameters

type Type

The type to get custom attributes for.

assemblyQualifiedTypeName string

The type of the attribute to find. Will accept attribute types that are concrete, closed generic, and open generic. When provided an open generic type (e.g., MyAttribute<>) it will return matching closed generic attributes (e.g., MyAttribute>int<)

Returns

IReadOnlyCollection<Attribute>

The matching attributes that decorate the type

GetMatchingCustomAttributes(Type, Type)

Gets all the custom attributes for the type that are of the given attribute type.

public static IReadOnlyCollection<Attribute> GetMatchingCustomAttributes(this Type type, Type attributeType)

Parameters

type Type

The type to get custom attributes for.

attributeType Type

The type of the attribute to find. Will accept attribute types that are concrete, closed generic, and open generic. When provided an open generic type (e.g., MyAttribute<>) it will return matching closed generic attributes (e.g., MyAttribute>int<)

Returns

IReadOnlyCollection<Attribute>

The matching attributes that decorate the type

Implements(Type, Type)

Determines if the given type implements the given interface.

public static bool Implements(this Type type, Type interfaceType)

Parameters

type Type

The type to check

interfaceType Type

The interface type to look for

Returns

bool

Returns true if the type implements the interface; false, otherwise

IsFromLocalAssembly(Type)

Determines if the given type is from a local assembly.

public static bool IsFromLocalAssembly(this Type type)

Parameters

type Type

The type to verify

Returns

bool

Returns true if the type originates in a local assembly; false if the type originates in the GAC.

IsNullable(Type)

Determines whether type is a nullable type; that is, whether it is a reference type or it represents Nullable<T> for a value type.

public static bool IsNullable(this Type type)

Parameters

type Type

Returns

bool

IsNullableEnum(Type)

Determines whether type represents a nullable enum value.

public static bool IsNullableEnum(this Type type)

Parameters

type Type

Returns

bool

ResolveGenericTypes(MethodInfo, object?[])

Resolves all the generic types for a test method. The arguments are used to determine the best matching generic types for the method that can be satisfied by all the generic parameters and their argument values.

public static Type[] ResolveGenericTypes(this MethodInfo method, object?[] arguments)

Parameters

method MethodInfo

The method

arguments object[]

The argument values being passed to the method

Returns

Type[]

The best matching generic types

ResolveMethodArguments(MethodBase, object?[])

Resolves argument values for the test method, ensuring they are the correct type, including support for optional method arguments.

public static object?[] ResolveMethodArguments(this MethodBase testMethod, object?[] arguments)

Parameters

testMethod MethodBase

The test method to resolve.

arguments object[]

The user-supplied method arguments.

Returns

object[]

The argument values

SafeName(Type)

Gets a fully qualified type name (i.e., FullName), falling back to a simple type name (i.e., Name) when a fully qualified name is not available. Typically used when presenting type names to the user, or to guarantee the type name is never null.

public static string SafeName(this Type type)

Parameters

type Type

Returns

string

ToCommaSeparatedList(IEnumerable<Type?>, string)

Convert a collection of Type objects into a comma-separated list for display purposes.

public static string ToCommaSeparatedList(this IEnumerable<Type?> types, string nullDisplay = "(null)")

Parameters

types IEnumerable<Type>
nullDisplay string

Returns

string

ToDisplayName(Type)

Converts a type into a name string for display purposes. It attempts to make a more user friendly name than FullName would give, especially when the type is generic.

public static string ToDisplayName(this Type type)

Parameters

type Type

Returns

string

ToSimpleName(Type)

Gets the simple name for a type, suitable for use with TestClassSimpleName.

public static string ToSimpleName(this Type type)

Parameters

type Type

Returns

string

ToVSTestTypeName(Type, MethodInfo?, Type?)

Converts a Type name into the correct form for VSTest managed type name for using in managed TestCase properties and by xunit.runner.visualstudio.

public static string ToVSTestTypeName(this Type type, MethodInfo? testMethod = null, Type? testClass = null)

Parameters

type Type
testMethod MethodInfo
testClass Type

Returns

string

Remarks

UnwrapNullable(Type)

Attempts to strip Nullable<T> from a type value and just return T. For non-nullable types, will return the type that was passed in.

public static Type UnwrapNullable(this Type type)

Parameters

type Type

Returns

Type