ReflectionUtilities Class

Summary

A variety of utility methods relating to reflection.
Assembly
Weaver.dll
Namespace
Weaver
Base Types
  • Object
graph BT Type-->Base0["Object"] Type["ReflectionUtilities"] class Type type-node

Syntax

public static class ReflectionUtilities

Fields

Name Constant Value Summary
AnyAccessBindings 60
System.Reflection.BindingFlags for any access modifiers.
static
InstanceBindings 52
System.Reflection.BindingFlags for instance access modifiers.
static
PublicInstanceBindings 20
System.Reflection.BindingFlags for public and instance.
static
PublicStaticBindings 24
System.Reflection.BindingFlags for public and static.
static
StaticBindings 56
System.Reflection.BindingFlags for static access modifiers.
static

Methods

Name Value Summary
AppendDeepToString(StringBuilder, int, Object, int) void
Builds a string containing the values of each of the specified object's fields.
static
CanBeConst(Type) bool
All built-in types except System.Object can be const.
static
DeepToString(Object) string
Builds a string containing the values of each of the specified object's fields.
static
ForEachCustomAttribute<TAttribute>(ICustomAttributeProvider, Action<TAttribute>, bool) bool
Calls `method` once for each attribute of the specified type on `target`.
static
ForEachType(Action<Type>) void
Calls the specified method once for each type in every currently loaded assembly.

Most situations where you want to do something for each type are in reference to something specific. For example: when searching for every type with a certain attribute, you only need to look in assemblies that reference the assembly containing the attribute so you should instead use Weaver.ReflectionUtilities.Weaver.ReflectionUtilities.Assemblies.ForEachTypeInDependantAssemblies(System.Reflection.Assembly,System.Action{System.Type}).
static
GetAttribute<TAttribute>(ICustomAttributeProvider, bool) TAttribute
Gets a single custom attribute of type T and casts it.
static
GetAttributedFields<TAttribute>(Type, BindingFlags, List<TAttribute>, List<FieldInfo>) void
Gets all fields with the specified attribute in `type`.
static
GetAttributedMethods<TAttribute>(BindingFlags, List<TAttribute>, List<MethodInfo>) void
Gets all methods with the specified attribute in every type in the currently loaded assemblies.
static
GetAttributedMethods<TAttribute>(Type, BindingFlags, List<TAttribute>, List<MethodInfo>) void
Gets all methods with the specified attribute in `type`.
static
GetAttributedProperties<TAttribute>(Type, BindingFlags, List<TAttribute>, List<PropertyInfo>) void
Gets all properties with the specified attribute in `type`.
static
GetAttributedTypes<TAttribute>(List<TAttribute>, List<Type>, bool) void
Gets all types with the specified attribute all currently loaded assemblies.
static
GetDelegate<T>(MethodInfo) T
Creates a delegate of the specified type from `method`.
static
GetDelegate<T>(MethodInfo, T) void
Creates a delegate of the specified type from `method`.
static
GetDerivedTypes(Type, bool) List<Type>
Gets all non-abstract types in the currently loaded assemblies which derive from the specified base type (starting with the base type itself if it isn't abstract).
static
GetDerivedTypes(Type, ICollection<Type>, bool) void
Gets all non-abstract types in the currently loaded assemblies which derive from the specified base type (starting with the base type itself if it isn't abstract).
static
GetGenericInterfaceArguments(Type, Type) Type[]
If `type` implements the `interfaceType` this method returns the generic arguments it uses for that interface.
static
GetInheritanceModifier(Type, string, Type[]) string
Returns null, "override" or "new" as appropriate for a method called `methodName` in a child of `baseType`.
static
GetMemberFunc<T>(Type, string, Object, string, BindingFlags) Func<T>
Tries to find a field/property/method in the `declaringType` with the `name` and creates a delegate to get its value.

If a member is found but can't be converted to an appropriate delegate, the `error` message describes the problem.
static
GetQualifiedName(Type) string
Returns "System.Type.FullName, System.Reflection.AssemblyName.Name".
static
HasFlags(BindingFlags, BindingFlags) bool
Checks if `bindings` contains all the flags in `flags`.
static
HasImplicitCast(Type, Type) bool
Returns true if `from` can be implicitly cast to `to`.
static
IsObsolete(ICustomAttributeProvider, bool) bool
Checks if `target` has an [System.ObsoleteAttribute].
static
IsSubclassOfGenericDefinition(Type, Type) bool
Checks if `type` is descended from `generic` (where `generic` is a generic type definition).
static
IsSubclassOfGenericDefinition(Type, Type, Type[]) bool
Checks if `type` is descended from `generic` (where `generic` is a generic type definition).
static
OneObject(Object) Object[]
Returns System.Object[1] { obj }.

The array is kept in a field marked with [System.ThreadStaticAttribute], so it is thread safe but cannot be used recursively within a single thread.
static
OneType(Type) Type[]
Returns System.Type[1] { type }.

The array is kept in a field marked with [System.ThreadStaticAttribute], so it is thread safe but cannot be used recursively within a single thread.
static
ToAccessModifier(BindingFlags) string
Returns a description of the access modifier associated with `bindings`
static
TwoObjects(Object, Object) Object[]
Returns System.Object[2] { obj0, obj1 }.

The array is kept in a field marked with [System.ThreadStaticAttribute], so it is thread safe but cannot be used recursively within a single thread.
static
TwoTypes(Type, Type) Type[]
Returns System.Type[2] { type0, type1 }.

The array is kept in a field marked with [System.ThreadStaticAttribute], so it is thread safe but cannot be used recursively within a single thread.
static