Show / Hide Table of Contents

Class ReflectionUtils

Inheritance
System.Object
ReflectionUtils
Namespace: Eco.Shared.Utils
Assembly: Eco.Shared.dll
Syntax
public static class ReflectionUtils : Object

Fields

DeclaredOnlyLookup

Declaration
public const BindingFlags DeclaredOnlyLookup
Field Value
Type Description
System.Reflection.BindingFlags

HierarchyBinding

Declaration
public const BindingFlags HierarchyBinding
Field Value
Type Description
System.Reflection.BindingFlags

HierarchyBindingWithStatic

Declaration
public const BindingFlags HierarchyBindingWithStatic
Field Value
Type Description
System.Reflection.BindingFlags

LocalBinding

Declaration
public const BindingFlags LocalBinding
Field Value
Type Description
System.Reflection.BindingFlags

LocalStaticBinding

Declaration
public const BindingFlags LocalStaticBinding
Field Value
Type Description
System.Reflection.BindingFlags

Methods

AllMembers(Type, Boolean)

Gets all properties, methods, and extension methods. Operates either on instance members or static members based on passed bool.

Declaration
public static IEnumerable<MemberInfo> AllMembers(this Type type, bool instanceMembers = true)
Parameters
Type Name Description
System.Type type
System.Boolean instanceMembers
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Reflection.MemberInfo>

Attribute(MemberInfo, Type, Boolean)

Declaration
public static Attribute Attribute(this MemberInfo member, Type type, bool inherit = true)
Parameters
Type Name Description
System.Reflection.MemberInfo member
System.Type type
System.Boolean inherit
Returns
Type Description
System.Attribute

Attribute<T>(MemberInfo, Boolean)

Returns first attribute of type T if exists, otherwise null.

Declaration
public static T Attribute<T>(this MemberInfo member, bool inherit = true)
    where T : Attribute
Parameters
Type Name Description
System.Reflection.MemberInfo member
System.Boolean inherit
Returns
Type Description
T
Type Parameters
Name Description
T

AttributeIncludingInterfaces<T>(MemberInfo)

Declaration
public static T AttributeIncludingInterfaces<T>(this MemberInfo member)
    where T : Attribute
Parameters
Type Name Description
System.Reflection.MemberInfo member
Returns
Type Description
T
Type Parameters
Name Description
T

Attributes<T>(MemberInfo, Boolean)

Declaration
public static IEnumerable<T> Attributes<T>(this MemberInfo member, bool inherit = true)
    where T : Attribute
Parameters
Type Name Description
System.Reflection.MemberInfo member
System.Boolean inherit
Returns
Type Description
System.Collections.Generic.IEnumerable<T>
Type Parameters
Name Description
T

BasestOrDefault(Type, Func<Type, Boolean>)

Returns basest type in hierarchy matching predicate. All types higher in hierarchy also should match the predicate. Usually may be used to find a base type which first implements an interface:

type.BasestOrDefault(t => typeof(IController).IsAssignableFrom(t)).

Declaration
public static Type BasestOrDefault(this Type type, Func<Type, bool> predicate)
Parameters
Type Name Description
System.Type type
System.Func<System.Type, System.Boolean> predicate
Returns
Type Description
System.Type

BlendProperties(ICloneable, ICloneable, Single)

Clones source, then assigns all int and float values a lerped-value relative to 'target'

Declaration
public static object BlendProperties(ICloneable source, ICloneable target, float lerp)
Parameters
Type Name Description
System.ICloneable source
System.ICloneable target
System.Single lerp
Returns
Type Description
System.Object

CallStatic(Type, String, Object[])

Call a static function on the given type.

Declaration
public static void CallStatic(this Type type, string name, params object[] p)
Parameters
Type Name Description
System.Type type
System.String name
System.Object[] p

CallStatic<T>(Type, String, Object[])

Call a static with an expected return type T

Declaration
public static T CallStatic<T>(this Type type, string name, params object[] p)
Parameters
Type Name Description
System.Type type
System.String name
System.Object[] p
Returns
Type Description
T
Type Parameters
Name Description
T

CanAccess(Assembly, FieldInfo)

Checks if field may be accessed (without reflection) from assembly. It is possible in 3 cases:

  • field is "public";
  • field declared in the assembly;
  • field declared as "protected" or "protected internal" and field's owning type is in the assembly; It currently doesn't support intermediate types through inheritance, i.e. in A (Assembly 1) : B (Assembly 2) : C (Assembly 3) if you check for Assembly 2 for A.field which is declared as protected in C then it will return false. Adding this check will make logic more complex and doesn't have practical usage for now.
Declaration
public static bool CanAccess(this Assembly assembly, FieldInfo field)
Parameters
Type Name Description
System.Reflection.Assembly assembly
System.Reflection.FieldInfo field
Returns
Type Description
System.Boolean

CanAccess(Assembly, MethodInfo)

Checks if method may be accessed (without reflection) from assembly. It is possible in 3 cases:

  • method is "public";
  • method declared in the assembly;
  • method declared as "protected" or "protected internal" and method's owning type is in the assembly; It currently doesn't support intermediate types through inheritance, i.e. in A (Assembly 1) : B (Assembly 2) : C (Assembly 3) if you check for Assembly 2 for A.Method which is declared as protected in C then it will return false. Adding this check will make logic more complex and doesn't have practical usage for now.
Declaration
public static bool CanAccess(this Assembly assembly, MethodInfo method)
Parameters
Type Name Description
System.Reflection.Assembly assembly
System.Reflection.MethodInfo method
Returns
Type Description
System.Boolean

ConcreteTypes(Type, Assembly, Boolean)

Declaration
public static IEnumerable<Type> ConcreteTypes(this Type baseType, Assembly assembly = null, bool includeSelf = false)
Parameters
Type Name Description
System.Type baseType
System.Reflection.Assembly assembly
System.Boolean includeSelf
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Type>

ConcreteTypesWithInteface(Type, IEnumerable<Assembly>)

Returns all types that implements interface interfaceType

Declaration
public static IEnumerable<Type> ConcreteTypesWithInteface(this Type interfaceType, IEnumerable<Assembly> assemblies = null)
Parameters
Type Name Description
System.Type interfaceType
System.Collections.Generic.IEnumerable<System.Reflection.Assembly> assemblies
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Type>

CreatableTypes(Type, Assembly, Boolean)

Returns types that derive from this type in the given assembly (or all assemblies if null), and optionally the passed type itself, if it's creatable.

Declaration
public static IEnumerable<Type> CreatableTypes(this Type baseType, Assembly assembly = null, bool includeSelf = false)
Parameters
Type Name Description
System.Type baseType
System.Reflection.Assembly assembly
System.Boolean includeSelf
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Type>

DeepAttributes(Type, Type)

Collects all attributes of 'attributeToScan' in typeToScan itself and its Fields, Properties and Methods

Declaration
public static List<(MemberInfo, object)> DeepAttributes(this Type typeToScan, Type attributeToScan)
Parameters
Type Name Description
System.Type typeToScan
System.Type attributeToScan

Attribute to scan for.

Returns
Type Description
System.Collections.Generic.List<System.ValueTuple<System.Reflection.MemberInfo, System.Object>>

Discovered MemberInfo and their attribute data.

DerivedTypes(IEnumerable<Assembly>, Type, Boolean)

Returns all derived types for baseType in assemblies optionally (if includeSelf set) returning the type itself.

Declaration
public static IEnumerable<Type> DerivedTypes(this IEnumerable<Assembly> assemblies, Type baseType, bool includeSelf = false)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<System.Reflection.Assembly> assemblies
System.Type baseType
System.Boolean includeSelf
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Type>

DerivedTypes(Type, Assembly, Boolean)

Returns all derived types for baseType optionally in specific assembly and optionally (if includeSelf set) returning the type itself.

Declaration
public static IEnumerable<Type> DerivedTypes(this Type baseType, Assembly assembly = null, bool includeSelf = false)
Parameters
Type Name Description
System.Type baseType
System.Reflection.Assembly assembly
System.Boolean includeSelf
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Type>

DerivedTypes<T>(IEnumerable<Assembly>, Boolean)

Declaration
public static IEnumerable<Type> DerivedTypes<T>(this IEnumerable<Assembly> assemblies, bool includeSelf = false)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<System.Reflection.Assembly> assemblies
System.Boolean includeSelf
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Type>
Type Parameters
Name Description
T

DerivesFrom(Type, Type)

Declaration
public static bool DerivesFrom(this Type derivedType, Type baseType)
Parameters
Type Name Description
System.Type derivedType
System.Type baseType
Returns
Type Description
System.Boolean

DerivesFrom<TBase>(Type)

Declaration
public static bool DerivesFrom<TBase>(this Type derivedType)
Parameters
Type Name Description
System.Type derivedType
Returns
Type Description
System.Boolean
Type Parameters
Name Description
TBase

ForEachMemberWithAttribute<T>(Type, Boolean, Action<MemberInfo, T>)

Execute action for every member of type that has argument of type T.

Declaration
public static void ForEachMemberWithAttribute<T>(this Type type, bool includeExtensions, Action<MemberInfo, T> action)
    where T : Attribute
Parameters
Type Name Description
System.Type type
System.Boolean includeExtensions

Should extension methods be included?

System.Action<System.Reflection.MemberInfo, T> action
Type Parameters
Name Description
T

GenericTypeImplementations(Type, Assembly)

Returns all compile time implementations of genericTypeDefinition.

Declaration
public static IEnumerable<Type> GenericTypeImplementations(this Type genericTypeDefinition, Assembly assembly)
Parameters
Type Name Description
System.Type genericTypeDefinition
System.Reflection.Assembly assembly
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Type>

GetAllMembers(Type, BindingFlags)

Get the properties on this type, and all properties in interface hierarchies it implements.

Declaration
public static IEnumerable<MemberInfo> GetAllMembers(this Type type, BindingFlags flags)
Parameters
Type Name Description
System.Type type
System.Reflection.BindingFlags flags
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Reflection.MemberInfo>

GetDefaultValue(Type)

Returns default value for type.

Declaration
public static object GetDefaultValue(Type type)
Parameters
Type Name Description
System.Type type
Returns
Type Description
System.Object

Same as default(T) via reflection.

GetEnumerableType(IEnumerable)

Declaration
public static Type GetEnumerableType(this IEnumerable enumeration)
Parameters
Type Name Description
System.Collections.IEnumerable enumeration
Returns
Type Description
System.Type

GetMatchingMethod(MethodInfo, Type)

Returns method with same name and arguments in type (looking into declared members only). Returns null if such method not found.

Declaration
public static MethodInfo GetMatchingMethod(this MethodInfo methodInfoInTypeDefinition, Type type)
Parameters
Type Name Description
System.Reflection.MethodInfo methodInfoInTypeDefinition
System.Type type
Returns
Type Description
System.Reflection.MethodInfo

GetMemberFromAll(Type, String, BindingFlags)

Get a member from a given type, and include all the sub interfaces too, which usually arent included with default GetMember.

Declaration
public static MemberInfo GetMemberFromAll(this Type type, string name, BindingFlags flags)
Parameters
Type Name Description
System.Type type
System.String name
System.Reflection.BindingFlags flags
Returns
Type Description
System.Reflection.MemberInfo

GetMethodRealType(MethodInfo)

If the method is an extended one we get the extension type else we just get the method declaring type.

Declaration
public static Type GetMethodRealType(this MethodInfo methodInfo)
Parameters
Type Name Description
System.Reflection.MethodInfo methodInfo
Returns
Type Description
System.Type

GetPropertyValueByName<T>(Object, String)

Declaration
public static T GetPropertyValueByName<T>(this object obj, string name)
Parameters
Type Name Description
System.Object obj
System.String name
Returns
Type Description
T
Type Parameters
Name Description
T

GetPublicProperties(Type, BindingFlags)

Get the properties on this type, and all properties in interface hierarchies it implements.

Declaration
public static IEnumerable<PropertyInfo> GetPublicProperties(this Type type, BindingFlags flags)
Parameters
Type Name Description
System.Type type
System.Reflection.BindingFlags flags
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Reflection.PropertyInfo>

GetSimpleName(MemberInfo)

Returns simple name (without explicit interface specification). I.e. Eco.Core.Systems.IRegistrar.IdToObj => IdToObj.

Declaration
public static string GetSimpleName(this MemberInfo member)
Parameters
Type Name Description
System.Reflection.MemberInfo member
Returns
Type Description
System.String

GetSimpleName(String)

Returns simple name (without explicit interface specification). I.e. Eco.Core.Systems.IRegistrar.IdToObj => IdToObj.

Declaration
public static string GetSimpleName(string name)
Parameters
Type Name Description
System.String name
Returns
Type Description
System.String

GetStaticFieldValue<T>(Type, String)

Returns value of static field on type.

Declaration
public static T GetStaticFieldValue<T>(this Type type, string fieldName)
Parameters
Type Name Description
System.Type type
System.String fieldName
Returns
Type Description
T
Type Parameters
Name Description
T

GetStaticFieldValue<TType, TFieldType>(String)

Returns value of static field on TType.

Declaration
public static TFieldType GetStaticFieldValue<TType, TFieldType>(string fieldName)
Parameters
Type Name Description
System.String fieldName
Returns
Type Description
TFieldType
Type Parameters
Name Description
TType
TFieldType

GetStringPropertyByName(Object, String)

Declaration
public static string GetStringPropertyByName(this object obj, string name)
Parameters
Type Name Description
System.Object obj
System.String name
Returns
Type Description
System.String

GetStructPropertyByName<T>(Object, String)

Declaration
public static T GetStructPropertyByName<T>(this object obj, string name)
    where T : struct, ValueType
Parameters
Type Name Description
System.Object obj
System.String name
Returns
Type Description
T
Type Parameters
Name Description
T

GetTypeByGenericTypeDefinitionInHierarchy(Type, Type)

Returns (if exists) type corresponding to genericTypeDefinition in type hierarchy. I.e. if when check for List{}: List{string} -> List{string}, IntList : List{int} -> List{int}.

Declaration
public static Type GetTypeByGenericTypeDefinitionInHierarchy(this Type type, Type genericTypeDefinition)
Parameters
Type Name Description
System.Type type
System.Type genericTypeDefinition
Returns
Type Description
System.Type

GetTypeFromFullName(String)

Declaration
public static Type GetTypeFromFullName(string typeName)
Parameters
Type Name Description
System.String typeName
Returns
Type Description
System.Type

GetTypeMemberInfo(Type, BindingFlags)

Declaration
public static IEnumerable<MemberInfo> GetTypeMemberInfo(this Type typeToScan, BindingFlags flags)
Parameters
Type Name Description
System.Type typeToScan
System.Reflection.BindingFlags flags
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Reflection.MemberInfo>

GetTypesWithAttribute<T>(Boolean)

Declaration
public static IEnumerable<Type> GetTypesWithAttribute<T>(bool inherited)
Parameters
Type Name Description
System.Boolean inherited
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Type>
Type Parameters
Name Description
T

GetTypesWithAttribute<T>(Assembly)

Returns all types in the assembly with the attribute,

Declaration
public static IEnumerable<Type> GetTypesWithAttribute<T>(this Assembly assembly)
Parameters
Type Name Description
System.Reflection.Assembly assembly

Assembly to check,

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Type>

System.Collections.Generic.IEnumerable<> containing all the found types.

Type Parameters
Name Description
T

Attribute to find,

HasAnyAttribute(MemberInfo, Type[])

Declaration
public static bool HasAnyAttribute(this MemberInfo member, params Type[] attributes)
Parameters
Type Name Description
System.Reflection.MemberInfo member
System.Type[] attributes
Returns
Type Description
System.Boolean

HasAttribute<T>(MemberInfo, Boolean)

Declaration
public static bool HasAttribute<T>(this MemberInfo member, bool inherit = true)
    where T : Attribute
Parameters
Type Name Description
System.Reflection.MemberInfo member
System.Boolean inherit
Returns
Type Description
System.Boolean
Type Parameters
Name Description
T

HasAttribute<T>(MemberInfo, Func<T, Boolean>, Boolean)

Declaration
public static bool HasAttribute<T>(this MemberInfo member, Func<T, bool> predicate, bool inherit = true)
    where T : Attribute
Parameters
Type Name Description
System.Reflection.MemberInfo member
System.Func<T, System.Boolean> predicate
System.Boolean inherit
Returns
Type Description
System.Boolean
Type Parameters
Name Description
T

HasAttributeIncludingInterfaces<T>(PropertyInfo)

Checks if property has attribute T or any of interfaces declaring same property has the attribute.

Declaration
public static bool HasAttributeIncludingInterfaces<T>(this PropertyInfo property)
    where T : Attribute
Parameters
Type Name Description
System.Reflection.PropertyInfo property
Returns
Type Description
System.Boolean
Type Parameters
Name Description
T

HasDefaultConstructor(Type)

Declaration
public static bool HasDefaultConstructor(this Type t)
Parameters
Type Name Description
System.Type t
Returns
Type Description
System.Boolean

HasGenericParameters(Type)

Checks if type has any generic parameters. I.e. when generic type used as based it isn't more generic type definition, but still has generic parameters in it's definition (i.e. base class for StringKeyDictionary{TValue} : Dictionary{string, TValue}).

Declaration
public static bool HasGenericParameters(this Type type)
Parameters
Type Name Description
System.Type type
Returns
Type Description
System.Boolean

InNamespace(Type, String)

Checks if type in namespace (same or nested).

Declaration
public static bool InNamespace(this Type type, string namespace)
Parameters
Type Name Description
System.Type type
System.String namespace
Returns
Type Description
System.Boolean

IsAnonymous(Action)

Declaration
public static bool IsAnonymous(this Action action)
Parameters
Type Name Description
System.Action action
Returns
Type Description
System.Boolean

IsBackingField(FieldInfo)

Checks if fieldInfo is backing field (for auto-property).

Declaration
public static bool IsBackingField(this FieldInfo fieldInfo)
Parameters
Type Name Description
System.Reflection.FieldInfo fieldInfo
Returns
Type Description
System.Boolean

IsExtension(MethodInfo)

Checks if methodInfo is an extension method.

Declaration
public static bool IsExtension(this MethodInfo methodInfo)
Parameters
Type Name Description
System.Reflection.MethodInfo methodInfo
Returns
Type Description
System.Boolean

IsGenericTypeConstructedFrom(Type, Type)

Checks if genericType is a generic type constructed from genericTypeDefinition.

Declaration
public static bool IsGenericTypeConstructedFrom(this Type genericType, Type genericTypeDefinition)
Parameters
Type Name Description
System.Type genericType
System.Type genericTypeDefinition
Returns
Type Description
System.Boolean

IsHidden(Type)

Declaration
public static bool IsHidden(this Type t)
Parameters
Type Name Description
System.Type t
Returns
Type Description
System.Boolean

IsInheritFromGenericTypeDefinition(Type, Type)

Checks if any base class is generic type from generic type definition.

Declaration
public static bool IsInheritFromGenericTypeDefinition(this Type type, Type genericTypeDefinition)
Parameters
Type Name Description
System.Type type
System.Type genericTypeDefinition
Returns
Type Description
System.Boolean

IsOverride(MethodInfo)

Checks if methodInfo is an method override.

Declaration
public static bool IsOverride(this MethodInfo methodInfo)
Parameters
Type Name Description
System.Reflection.MethodInfo methodInfo
Returns
Type Description
System.Boolean

IsParentOf(Type, Type)

Declaration
public static bool IsParentOf(this Type baseType, Type derivedType)
Parameters
Type Name Description
System.Type baseType
System.Type derivedType
Returns
Type Description
System.Boolean

IsParentOf<TDerived>(Type)

Declaration
public static bool IsParentOf<TDerived>(this Type baseType)
Parameters
Type Name Description
System.Type baseType
Returns
Type Description
System.Boolean
Type Parameters
Name Description
TDerived

IsSerialized(MemberInfo)

Check member is serialized, through SerializedAttribute, EcoAttribute or SerializedDerivedAttribute

Declaration
public static bool IsSerialized(this MemberInfo member)
Parameters
Type Name Description
System.Reflection.MemberInfo member
Returns
Type Description
System.Boolean

IsSerializedTypeName(MemberInfo)

Checks if System.Type may be referenced in serialized data, but may not be serialized itself. Any IsSerialized(MemberInfo) type is also IsSerializedTypeName(MemberInfo), but not all IsSerializedTypeName(MemberInfo) types are IsSerialized(MemberInfo).

Declaration
public static bool IsSerializedTypeName(this MemberInfo member)
Parameters
Type Name Description
System.Reflection.MemberInfo member
Returns
Type Description
System.Boolean

MembersWithAttribute<T>(Type, Boolean)

Returns all public instance and static members that have the passed attribute, including from parent classes.

Declaration
public static IEnumerable<(MemberInfo, T)> MembersWithAttribute<T>(this Type type, bool includeExtensions = true)
    where T : Attribute
Parameters
Type Name Description
System.Type type
System.Boolean includeExtensions
Returns
Type Description
System.Collections.Generic.IEnumerable<System.ValueTuple<System.Reflection.MemberInfo, T>>
Type Parameters
Name Description
T

MethodsWithAttribute<T>(Type)

Declaration
public static IEnumerable<MethodInfo> MethodsWithAttribute<T>(this Type type)
    where T : Attribute
Parameters
Type Name Description
System.Type type
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Reflection.MethodInfo>
Type Parameters
Name Description
T

NameNoGeneric(MemberInfo)

Declaration
public static string NameNoGeneric(this MemberInfo info)
Parameters
Type Name Description
System.Reflection.MemberInfo info
Returns
Type Description
System.String

NameNoGeneric(String)

Declaration
public static string NameNoGeneric(this string type)
Parameters
Type Name Description
System.String type
Returns
Type Description
System.String

NameNoGeneric(Type)

Declaration
public static string NameNoGeneric(this Type type)
Parameters
Type Name Description
System.Type type
Returns
Type Description
System.String

PrepareCache(Type, Type)

Declaration
public static void PrepareCache(Type type, Type attributeType)
Parameters
Type Name Description
System.Type type
System.Type attributeType

PropertyValue<T>(Object, PropertyInfo)

Returns a value of property converted to T.

Declaration
public static T PropertyValue<T>(this object obj, PropertyInfo property)
Parameters
Type Name Description
System.Object obj
System.Reflection.PropertyInfo property
Returns
Type Description
T
Type Parameters
Name Description
T

ReflectionToString(StringBuilder, Object)

Add details on each of the properties and fields of the passed object.

Declaration
public static void ReflectionToString(StringBuilder sb, object obj)
Parameters
Type Name Description
System.Text.StringBuilder sb
System.Object obj

SetPropertyByName(Object, String, Object)

Declaration
public static bool SetPropertyByName(this object obj, string name, object value)
Parameters
Type Name Description
System.Object obj
System.String name
System.Object value
Returns
Type Description
System.Boolean

TryCallStatic(Type, String, Object[])

Try to call a static method, doing nothing if it doesn't exist or is wrong signature.

Declaration
public static void TryCallStatic(this Type type, string name, params object[] p)
Parameters
Type Name Description
System.Type type
System.String name
System.Object[] p

TryCallStatic<T>(Type, String, Object[])

Call a static function of the given name, triggering an error if it doesnt exist. T = return type

Declaration
public static T TryCallStatic<T>(this Type type, string name, params object[] p)
Parameters
Type Name Description
System.Type type
System.String name
System.Object[] p
Returns
Type Description
T
Type Parameters
Name Description
T

TryGetAttribute<T>(MemberInfo, Boolean, out T)

Declaration
public static bool TryGetAttribute<T>(this MemberInfo member, bool inherit, out T attr)
    where T : Attribute
Parameters
Type Name Description
System.Reflection.MemberInfo member
System.Boolean inherit
T attr
Returns
Type Description
System.Boolean
Type Parameters
Name Description
T

TryGetExtensionType(MethodInfo, out Type)

Try to get the extension type of the method if it's an extended method else return false.

Declaration
public static bool TryGetExtensionType(this MethodInfo methodInfo, out Type type)
Parameters
Type Name Description
System.Reflection.MethodInfo methodInfo
System.Type type
Returns
Type Description
System.Boolean

TryGetGenericInterface(Type, Type, out Type)

Tries to get genericInterface constructed from genericInterfaceDefinition implemented by type.

Declaration
public static bool TryGetGenericInterface(this Type type, Type genericInterfaceDefinition, out Type genericInterface)
Parameters
Type Name Description
System.Type type
System.Type genericInterfaceDefinition
System.Type genericInterface
Returns
Type Description
System.Boolean

TryGetMatchingInterfaceMethod(MethodInfo, Func<MethodInfo, Boolean>, out MethodInfo)

Tries to get first interface declaring methodInfo matching predicate.

Declaration
public static bool TryGetMatchingInterfaceMethod(MethodInfo methodInfo, Func<MethodInfo, bool> predicate, out MethodInfo interfaceMethodInfo)
Parameters
Type Name Description
System.Reflection.MethodInfo methodInfo
System.Func<System.Reflection.MethodInfo, System.Boolean> predicate
System.Reflection.MethodInfo interfaceMethodInfo
Returns
Type Description
System.Boolean

TryGetMatchingMethodWithInheritance(MethodInfo, Func<MethodInfo, Boolean>, out MethodInfo)

Searches matching method starting from methodInfo and then fallback to base class and interfaces. I.e. may be used to find base method in non-generic type.

Declaration
public static bool TryGetMatchingMethodWithInheritance(MethodInfo methodInfo, Func<MethodInfo, bool> predicate, out MethodInfo matchingMethodInfo)
Parameters
Type Name Description
System.Reflection.MethodInfo methodInfo
System.Func<System.Reflection.MethodInfo, System.Boolean> predicate
System.Reflection.MethodInfo matchingMethodInfo
Returns
Type Description
System.Boolean

TryGetPropertyValueByName<T>(Object, String, out T)

Try to get a property using reflection for the given name, and assign it to val, returning true if found.

Declaration
public static bool TryGetPropertyValueByName<T>(this object obj, string name, out T val)
Parameters
Type Name Description
System.Object obj
System.String name
T val
Returns
Type Description
System.Boolean
Type Parameters
Name Description
T

Value(MemberInfo, Object, Object[])

Declaration
public static object Value(this MemberInfo member, object o, params object[] parameters)
Parameters
Type Name Description
System.Reflection.MemberInfo member
System.Object o
System.Object[] parameters
Returns
Type Description
System.Object

Value<T>(MemberInfo, Object, Object[])

Declaration
public static T Value<T>(this MemberInfo member, object o, params object[] parameters)
Parameters
Type Name Description
System.Reflection.MemberInfo member
System.Object o
System.Object[] parameters
Returns
Type Description
T
Type Parameters
Name Description
T

Value<T>(PropertyInfo, Object)

Returns a value of property converted to T.

Declaration
public static T Value<T>(this PropertyInfo property, object obj)
Parameters
Type Name Description
System.Reflection.PropertyInfo property
System.Object obj
Returns
Type Description
T
Type Parameters
Name Description
T

VerifySignature(MethodInfo, Type[])

Return true if the given method has the exact list of types in this order as its signature.

Declaration
public static bool VerifySignature(this MethodInfo method, params Type[] types)
Parameters
Type Name Description
System.Reflection.MethodInfo method
System.Type[] types
Returns
Type Description
System.Boolean

WithAttribute<TAttr>(IEnumerable<PropertyInfo>, Boolean)

Syntax sugar for WithAttribute<TMember, TAttr>(IEnumerable<TMember>, Boolean) for property infos.

Declaration
public static IEnumerable<PropertyInfo> WithAttribute<TAttr>(this IEnumerable<PropertyInfo> properties, bool inherit = true)
    where TAttr : Attribute
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<System.Reflection.PropertyInfo> properties
System.Boolean inherit
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Reflection.PropertyInfo>
Type Parameters
Name Description
TAttr

WithAttribute<TMember, TAttr>(IEnumerable<TMember>, Boolean)

Filters members to keep only members with TAttr attribute(s).

Declaration
public static IEnumerable<TMember> WithAttribute<TMember, TAttr>(this IEnumerable<TMember> members, bool inherit = true)
    where TMember : MemberInfo where TAttr : Attribute
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<TMember> members
System.Boolean inherit
Returns
Type Description
System.Collections.Generic.IEnumerable<TMember>
Type Parameters
Name Description
TMember
TAttr

ZipByProperty<T>(Object, Object, Object, Func<T, T, T>)

Goes over each assignable member of type T in the given object, gets the value in both 'object' and 'other', and calls the func to transform them into the final value.

Declaration
public static void ZipByProperty<T>(this object target, object leftObj, object rightObj, Func<T, T, T> transformFunc)
    where T : new()
Parameters
Type Name Description
System.Object target

The given object.

System.Object leftObj

The object providing the left value in the transformFunc

System.Object rightObj

The object providing the right value in the transformFunc

System.Func<T, T, T> transformFunc

The function that accepts the property from both left and right, and returns the value of the new one.

Type Parameters
Name Description
T

The parameter type of properties we're looking at.

☀
☾
In This Article
Back to top
Copyright (c) Strange Loop Games 2021
☀
☾