Class ReflectionExtensions
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
typeTypeThe 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
methodMethodInfoThe test method
baseDisplayNamestringThe base part of the display name
argumentsobject[]The test method arguments
genericTypesType[]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
attributeAttributeThe attribute to get custom attributes for.
assemblyQualifiedTypeNamestringThe 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
attributeAttributeThe attribute to get custom attributes for.
attributeTypeTypeThe 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
assemblyAssemblyThe assembly to get custom attributes for.
assemblyQualifiedTypeNamestringThe 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
assemblyAssemblyThe assembly to get custom attributes for.
attributeTypeTypeThe 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
methodMethodInfoThe method to get custom attributes for.
assemblyQualifiedTypeNamestringThe 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
methodMethodInfoThe method to get custom attributes for.
attributeTypeTypeThe 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
parameterParameterInfoThe parameter to get custom attributes for.
assemblyQualifiedTypeNamestringThe 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
parameterParameterInfoThe parameter to get custom attributes for.
attributeTypeTypeThe 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
typeTypeThe type to get custom attributes for.
assemblyQualifiedTypeNamestringThe 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
typeTypeThe type to get custom attributes for.
attributeTypeTypeThe 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
Returns
- bool
Returns
trueif 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
typeTypeThe type to verify
Returns
- bool
Returns
trueif the type originates in a local assembly;falseif 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
typeType
Returns
IsNullableEnum(Type)
Determines whether type represents a nullable enum value.
public static bool IsNullableEnum(this Type type)
Parameters
typeType
Returns
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
methodMethodInfoThe method
argumentsobject[]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
testMethodMethodBaseThe test method to resolve.
argumentsobject[]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
typeType
Returns
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
typesIEnumerable<Type>nullDisplaystring
Returns
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
typeType
Returns
ToSimpleName(Type)
Gets the simple name for a type, suitable for use with TestClassSimpleName.
public static string ToSimpleName(this Type type)
Parameters
typeType
Returns
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
typeTypetestMethodMethodInfotestClassType
Returns
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
typeType