Table of Contents

Class ReflectionHelpers

Namespace
Steeltoe.Common.Reflection
Assembly
Steeltoe.Common.dll
public static class ReflectionHelpers
Inheritance
ReflectionHelpers
Inherited Members

Methods

CreateInstance(Type, object[])

Create an instance of a type

public static object CreateInstance(Type t, object[] args = null)

Parameters

t Type

Type to instantiate

args object[]

Constructor parameters

Returns

object

New instance of desired type

FindAssemblies(Func<Assembly, bool>)

Find assemblies matching a query

public static IEnumerable<Assembly> FindAssemblies(Func<Assembly, bool> assemblyQuery)

Parameters

assemblyQuery Func<Assembly, bool>

Your assembly search query

Returns

IEnumerable<Assembly>

Assemblies in CurrentDomain matching the query

FindAssembliesWithAttribute<T>()

Find a list of types specified in an assembly attribute

public static IEnumerable<Assembly> FindAssembliesWithAttribute<T>() where T : AssemblyContainsTypeAttribute

Returns

IEnumerable<Assembly>

A list of matching types

Type Parameters

T

The attribute that defines the types to get

FindAssembly(string)

Find an assembly

public static Assembly FindAssembly(string name)

Parameters

name string

Name of the assembly to find

Returns

Assembly

A representation of the assembly

FindAttributedTypesFromAssemblyAttribute<T>()

Finds a list of types with the attributed identified by T

public static IEnumerable<Type> FindAttributedTypesFromAssemblyAttribute<T>() where T : AssemblyContainsTypeAttribute

Returns

IEnumerable<Type>

Matching types from within matching assemblies

Type Parameters

T

The assembly attribute that defines the desired type

FindDescendantTypes(Func<Assembly, bool>, Type)

Find types from assemblies matching the query that are based on a common type

public static IEnumerable<Type> FindDescendantTypes(Func<Assembly, bool> assemblyQuery, Type baseType)

Parameters

assemblyQuery Func<Assembly, bool>

Your assembly search query

baseType Type

Base type to search for

Returns

IEnumerable<Type>

A list of types that have the given type as a base type

FindInterfacedTypesFromAssemblyAttribute<TAttribute>()

Finds a list of types implementing the interface identified by TAttribute

public static IEnumerable<Type> FindInterfacedTypesFromAssemblyAttribute<TAttribute>() where TAttribute : AssemblyContainsTypeAttribute

Returns

IEnumerable<Type>

Matching types from within matching assemblies

Type Parameters

TAttribute

The assembly attribute that defines the desired interface type

FindMethod(Type, string, Type[])

Find a method within a type

public static MethodInfo FindMethod(Type type, string methodName, Type[] parameters = null)

Parameters

type Type

The type to search

methodName string

The name of the method

parameters Type[]

(Optional) The parameters in the signature

Returns

MethodInfo

The method you're searching for

FindType(Assembly, string)

Find a type from within an assembly

public static Type FindType(Assembly assembly, string typeName)

Parameters

assembly Assembly

The assembly to search

typeName string

The name of the type to retrieve

Returns

Type

The type

FindType(string[], string[])

Search a list of assemblies for the first matching type

public static Type FindType(string[] assemblyNames, string[] typeNames)

Parameters

assemblyNames string[]

List of assembly names to search

typeNames string[]

List of suitable types

Returns

Type

An appropriate type

Remarks

Great for finding an implementation type that could have one or more names in one or more assemblies

FindTypeFromAssemblyAttribute<T>()

Find a type specified in an assembly attribute

public static IEnumerable<Type> FindTypeFromAssemblyAttribute<T>() where T : AssemblyContainsTypeAttribute

Returns

IEnumerable<Type>

A list of matching types. Won't return more than one type per assembly

Type Parameters

T

The attribute that defines the type to get

FindTypeOrThrow(string[], string[], string, string)

Search a list of assemblies for the first matching type

public static Type FindTypeOrThrow(string[] assemblyNames, string[] typeNames, string typeName, string assemblyShortDescription)

Parameters

assemblyNames string[]

List of assembly names to search

typeNames string[]

List of suitable types

typeName string

To use in exception

assemblyShortDescription string

Describe what might be missing

Returns

Type

An appropriate type

Remarks

Great for finding an implementation type that could have one or more names in one or more assemblies

Exceptions

Exception

When type isn't found

FindTypesWithAttributeFromAssemblyAttribute<TTypeAttribute, TAssemblyAttribute>()

Finds a list of assemblies with TTypeAttribute contained within assemblies with TAssemblyAttribute

public static IEnumerable<Type> FindTypesWithAttributeFromAssemblyAttribute<TTypeAttribute, TAssemblyAttribute>() where TTypeAttribute : Attribute where TAssemblyAttribute : AssemblyContainsTypeAttribute

Returns

IEnumerable<Type>

Matching types from within matching assemblies

Type Parameters

TTypeAttribute

The Type attribute to locate

TAssemblyAttribute

The Assembly-level attribute to use to filter the assembly list

FindTypesWithAttribute<T>(Assembly)

Finds a list of types with T

public static IEnumerable<Type> FindTypesWithAttribute<T>(Assembly assembly) where T : Attribute

Parameters

assembly Assembly

The assembly to search for the type(s)

Returns

IEnumerable<Type>

A list of types with the specified attribute

Type Parameters

T

Type of Attribute to search for

Invoke(MethodBase, object, object[])

Invoke a function

public static object Invoke(MethodBase member, object instance, object[] args)

Parameters

member MethodBase

The method to execute

instance object

Instance of an object, if required by the method

args object[]

Arguments to pass to the method

Returns

object

Results of method call

IsAssemblyLoaded(string)

Try to load an assembly

public static bool IsAssemblyLoaded(string assembly)

Parameters

assembly string

Name of the assembly

Returns

bool

Boolean indicating success/failure of finding the assembly

TrySetProperty(object, string, object)

Try to set a property on an object

public static void TrySetProperty(object obj, string property, object value)

Parameters

obj object

Object to set a value on

property string

Property to set

value object

Value to use