Table of Contents

Class Reflect

Namespace
NUnit.Framework.Internal
Assembly
nunit.framework.dll

Helper methods for inspecting a type by reflection.

Many of these methods take ICustomAttributeProvider as an argument to avoid duplication, even though certain attributes can only appear on specific types of members, like MethodInfo or Type.

In the case where a type is being examined for the presence of an attribute, interface or named member, the Reflect methods operate with the full name of the member being sought. This removes the necessity of the caller having a reference to the assembly that defines the item being sought and allows the NUnit core to inspect assemblies that reference an older version of the NUnit framework.

public static class Reflect
Inheritance
Reflect
Inherited Members

Methods

Construct(Type)

Invoke the default constructor on a Type

public static object Construct(Type type)

Parameters

type Type

The Type to be constructed

Returns

object

An instance of the Type

Construct(Type, object?[]?)

Invoke a constructor on a Type with arguments

public static object Construct(Type type, object?[]? arguments)

Parameters

type Type

The Type to be constructed

arguments object[]

Arguments to the constructor

Returns

object

An instance of the Type

GetDefaultIndexer(Type, Type[])

Returns the get accessor for the indexer.

public static MethodInfo? GetDefaultIndexer(Type type, Type[] indexerTypes)

Parameters

type Type

Type to reflect on for the indexer.

indexerTypes Type[]

List of indexer types that matches the indexer type order.

Returns

MethodInfo

The Get accessor

GetMemberIncludingFromBase(Type, string, BindingFlags)

Searches for the specified members, using the specified binding constraints.

public static MemberInfo[] GetMemberIncludingFromBase(this Type type, string name, BindingFlags flags)

Parameters

type Type

The type to find the members from.

name string

The string containing the name of the members to get.

flags BindingFlags

A bitwise combination of the enumeration values that specify how the search is conducted.

Returns

MemberInfo[]

Remarks

Similar to GetMember(string, BindingFlags) but also finds private static members from the base class.

GetUltimateShadowingProperty(Type, string, BindingFlags)

Selects the ultimate shadowing property just like dynamic would, rather than throwing AmbiguousMatchException for properties that shadow properties of a different property type which is what GetProperty(string, BindingFlags) does.

If you request both public and nonpublic properties, every public property is preferred over every nonpublic property. It would violate the principle of least surprise for a derived class’s implementation detail to be chosen over the public API for a type.

public static PropertyInfo? GetUltimateShadowingProperty(Type type, string name, BindingFlags bindingFlags)

Parameters

type Type

See GetProperty(string, BindingFlags).

name string

See GetProperty(string, BindingFlags).

bindingFlags BindingFlags

See GetProperty(string, BindingFlags).

Returns

PropertyInfo

HasMethodWithAttribute(Type, Type)

Examine a fixture type and return true if it has a method with a particular attribute.

public static bool HasMethodWithAttribute(Type fixtureType, Type attributeType)

Parameters

fixtureType Type

The type to examine

attributeType Type

The attribute Type to look for

Returns

bool

True if found, otherwise false

InvokeMethod(MethodInfo, object?)

Invoke a parameterless method returning void on an object.

public static object? InvokeMethod(MethodInfo method, object? fixture)

Parameters

method MethodInfo

A MethodInfo for the method to be invoked

fixture object

The object on which to invoke the method

Returns

object

InvokeMethod(MethodInfo, object?, params object?[]?)

Invoke a method, converting any TargetInvocationException to an NUnitException.

public static object? InvokeMethod(MethodInfo method, object? fixture, params object?[]? args)

Parameters

method MethodInfo

A MethodInfo for the method to be invoked

fixture object

The object on which to invoke the method

args object[]

The argument list for the method

Returns

object

The return value from the invoked method