Class ReflectionHelpers
This static class contains helpers and extensions that assist in common reflection based tasks.
Inheritance
Namespace: Cireson.Core.Common.Helpers
Assembly: Cireson.Core.Common.dll
Syntax
public static class ReflectionHelpers : object
Remarks
Admin, 8/24/2017.
Methods
View SourceCanCastAs(Type, Type)
Inverse implementation of IsAssignableFrom.
Declaration
public static bool CanCastAs(this Type type, Type checkType)
Parameters
Type | Name | Description |
---|---|---|
Type | type | . |
Type | checkType | . |
Returns
Type | Description |
---|---|
System.Boolean | True if we can cast as, false if not. |
Remarks
DSL extension just to make code a bit easier to read.
CanCastAs<T>(Type)
Inverse implementation of IsAssignableFrom.
Declaration
public static bool CanCastAs<T>(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | . |
Returns
Type | Description |
---|---|
System.Boolean | True if we can cast as, false if not. |
Type Parameters
Name | Description |
---|---|
T | . |
Remarks
DSL extension just to make code a bit easier to read.
Cast(IEnumerable, Type)
Casts all items in the list to the specified type.
Declaration
public static IEnumerable Cast(this IEnumerable list, Type targetType)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable | list | . |
Type | targetType | . |
Returns
Type | Description |
---|---|
IEnumerable | An IEnumerable. |
Remarks
Admin, 8/24/2017.
CreateGenericEnumerable(Type, Object[])
Creates an IEnumerable`1 of the extended type, and returns it as an IEnumerable.
Declaration
public static IEnumerable CreateGenericEnumerable(this Type genericType, params object[] items)
Parameters
Type | Name | Description |
---|---|---|
Type | genericType | . |
System.Object[] | items | . |
Returns
Type | Description |
---|---|
IEnumerable | The new generic enumerable. |
Remarks
Admin, 8/24/2017.
FullNamespaceName(Type)
Returns the namespace, and name of the given type.
Declaration
public static string FullNamespaceName(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type. |
Returns
Type | Description |
---|---|
System.String | System.String. |
Remarks
Admin, 8/24/2017.
GetAllImplementationsOfType(Assembly, Type, Boolean)
Gets the type of all implementations of the specified type from the assembly.
Declaration
public static IEnumerable<Type> GetAllImplementationsOfType(this Assembly assembly, Type type, bool includeAbstract = true)
Parameters
Type | Name | Description |
---|---|---|
Assembly | assembly | The assembly. |
Type | type | The type. |
System.Boolean | includeAbstract | (Optional) if set to |
Returns
Type | Description |
---|---|
IEnumerable<Type> | IEnumerable<Type>. |
Remarks
Admin, 8/24/2017.
GetAllImplementationsOfType(Type, Boolean)
Gets the type of all implementations of the specified type.
Declaration
public static IEnumerable<Type> GetAllImplementationsOfType(Type type, bool includeAbstract = true)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type. |
System.Boolean | includeAbstract | (Optional) if set to |
Returns
Type | Description |
---|---|
IEnumerable<Type> | IEnumerable<Type>. |
Remarks
Admin, 8/24/2017.
GetAllImplementationsOfType<TBaseType>(Boolean)
Gets the type of all implementations of the specified generic type.
Declaration
public static IEnumerable<Type> GetAllImplementationsOfType<TBaseType>(bool includeAbstract = true)
where TBaseType : class
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | includeAbstract | (Optional) if set to |
Returns
Type | Description |
---|---|
IEnumerable<Type> | IEnumerable<Type>. |
Type Parameters
Name | Description |
---|---|
TBaseType | The base type. |
Remarks
Admin, 8/24/2017.
GetAllMethodsImplmentingAttribute(Type)
Gets all methods of all assemblies in the current AppDomain that implement a particualr attribute.
Declaration
public static IEnumerable<MethodInfo> GetAllMethodsImplmentingAttribute(Type attributeType)
Parameters
Type | Name | Description |
---|---|---|
Type | attributeType | . |
Returns
Type | Description |
---|---|
IEnumerable<MethodInfo> | IEnumerable{MethodInfo} |
Remarks
Admin, 8/24/2017.
GetAllMethodsImplmentingAttribute(Type, Object)
Gets all methods of all assemblies in the current AppDomain that implement a particualr attribute.
Declaration
public static IEnumerable<MethodInfo> GetAllMethodsImplmentingAttribute(Type attributeType, object target)
Parameters
Type | Name | Description |
---|---|---|
Type | attributeType | . |
System.Object | target | Target for the. |
Returns
Type | Description |
---|---|
IEnumerable<MethodInfo> | IEnumerable{MethodInfo} |
Remarks
Admin, 8/24/2017.
GetAllTypesImplementingAttribute(Type)
Gets all types implementing attributes in this collection.
Declaration
public static IEnumerable<Type> GetAllTypesImplementingAttribute(Type attributeType)
Parameters
Type | Name | Description |
---|---|---|
Type | attributeType | . |
Returns
Type | Description |
---|---|
IEnumerable<Type> | An enumerator that allows foreach to be used to process all types implementing attributes in this collection. |
Remarks
Admin, 8/24/2017.
GetAssemblySimpleName(String)
Returns the Simple name of an assembly given the fully qualified name.
Declaration
public static string GetAssemblySimpleName(string fullName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fullName | . |
Returns
Type | Description |
---|---|
System.String | The assembly simple name. |
Remarks
Admin, 8/24/2017.
GetClosestMethodMatch(Type, String, String)
A Type extension method that gets closest method match.
Declaration
public static MethodInfo GetClosestMethodMatch(this Type type, string methodName, string parametersJson = "")
Parameters
Type | Name | Description |
---|---|---|
Type | type | . |
System.String | methodName | . |
System.String | parametersJson | (Optional) |
Returns
Type | Description |
---|---|
MethodInfo | The closest method match. |
Remarks
Admin, 8/24/2017.
GetDefaultValue(Type)
Gets the default value of the given type.
Declaration
public static object GetDefaultValue(this Type t)
Parameters
Type | Name | Description |
---|---|---|
Type | t | The type. |
Returns
Type | Description |
---|---|
System.Object | System.Object. |
Remarks
Admin, 8/24/2017.
GetDependantAssemblies(Assembly, HashSet<AssemblyName>)
Returns all the referenced assemblies in the assembly, following the reference chain.
Declaration
public static HashSet<AssemblyName> GetDependantAssemblies(this Assembly assembly, HashSet<AssemblyName> foundAssemblies = null)
Parameters
Type | Name | Description |
---|---|---|
Assembly | assembly | . |
HashSet<AssemblyName> | foundAssemblies | (Optional) |
Returns
Type | Description |
---|---|
HashSet<AssemblyName> | The dependant assemblies. |
Remarks
Admin, 8/24/2017.
GetGenericArgumentFromSelfOrBase(Type)
Returns the single generic type argument from the given type, or it's inheritance chain.
Declaration
public static Type GetGenericArgumentFromSelfOrBase(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type to be inspected. |
Returns
Type | Description |
---|---|
Type | System.Type. |
Remarks
Admin, 8/24/2017.
GetGenericArgumentsFromSelfOrBase(Type)
Returns all the generic type arguments down the inheritance chain.
Declaration
public static IEnumerable<Type> GetGenericArgumentsFromSelfOrBase(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | . |
Returns
Type | Description |
---|---|
IEnumerable<Type> | An enumerator that allows foreach to be used to process the generic arguments from self or bases in this collection. |
Remarks
This is useful for determining type-specifing handling for actions that may inherit from generic actions that are not themselves generics.
GetGenericMethodInfo(Object, String, Type[])
Returns the method info for a generic method on the type based on the exact generic arument match. This is helpful for reflecting on methods that have overloaded generic methods.
Declaration
public static MethodInfo GetGenericMethodInfo(this object obj, string methodName, params Type[] typeArguments)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | . |
System.String | methodName | . |
Type[] | typeArguments | . |
Returns
Type | Description |
---|---|
MethodInfo | The generic method information. |
Remarks
Admin, 8/24/2017.
GetGenericMethodInfo(Type, String, Type[])
Returns the method info for a generic method on the type based on the exact generic arument match. This is helpful for reflecting on methods that have overloaded generic methods.
Declaration
public static MethodInfo GetGenericMethodInfo(this Type type, string methodName, params Type[] typeArguments)
Parameters
Type | Name | Description |
---|---|---|
Type | type | . |
System.String | methodName | . |
Type[] | typeArguments | Array of type arguments that match the method signature that is being searched for. |
Returns
Type | Description |
---|---|
MethodInfo | The generic method information. |
Remarks
Admin, 8/24/2017.
GetGenericMethodInfoByArgCount(Type, String, Type[])
Returns the method info for a generic method on the type based on the number of generic arguments. Gets the first generic method matching the number of arguments, but does not check for argument types. This is helpful for reflecting on methods that have overloaded generic methods.
Declaration
public static MethodInfo GetGenericMethodInfoByArgCount(this Type type, string methodName, params Type[] typeArguments)
Parameters
Type | Name | Description |
---|---|---|
Type | type | . |
System.String | methodName | . |
Type[] | typeArguments | . |
Returns
Type | Description |
---|---|
MethodInfo | The generic method information by argument count. |
Remarks
Admin, 8/24/2017.
GetLoadedAssembly(System.AppDomain, String)
Returns the loaded assembly specified by full name, or simple name.
Declaration
public static Assembly GetLoadedAssembly(this System.AppDomain domain, string assemblyFullName)
Parameters
Type | Name | Description |
---|---|---|
System.AppDomain | domain | . |
System.String | assemblyFullName | . |
Returns
Type | Description |
---|---|
Assembly | The loaded assembly. |
Remarks
Admin, 8/24/2017.
GetLocation(Assembly)
An Assembly extension method that gets a location.
Declaration
public static string GetLocation(this Assembly assembly)
Parameters
Type | Name | Description |
---|---|---|
Assembly | assembly | The assembly. |
Returns
Type | Description |
---|---|
System.String | The location. |
Remarks
Admin, 8/24/2017.
GetNestedGenericArguments(Type)
Returns a list of all types involved in the generic type arguments, this is useful for registering complex types to edm.
Declaration
public static IEnumerable<Type> GetNestedGenericArguments(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | . |
Returns
Type | Description |
---|---|
IEnumerable<Type> | An enumerator that allows foreach to be used to process the nested generic arguments in this collection. |
Remarks
Admin, 8/24/2017.
GetNullableType(Type)
Gets the nullable equivalent of the given type, if a value type.
Declaration
public static Type GetNullableType(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | . |
Returns
Type | Description |
---|---|
Type | Type. |
Remarks
Admin, 8/24/2017.
GetParameterNames(String)
retrieves a list of parameter names based on the parametersJson string.
Declaration
public static IEnumerable<string> GetParameterNames(string parametersJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | parametersJson | . |
Returns
Type | Description |
---|---|
IEnumerable<System.String> | An enumerator that allows foreach to be used to process the parameter names in this collection. |
Remarks
Admin, 8/24/2017.
GetParameterValues(MethodInfo, String)
Gets the parameter values in this collection.
Declaration
public static IEnumerable<Object> GetParameterValues(this MethodInfo method, string parameters = "")
Parameters
Type | Name | Description |
---|---|---|
MethodInfo | method | The method to act on. |
System.String | parameters | (Optional) Options for controlling the operation. |
Returns
Type | Description |
---|---|
IEnumerable<Object> | An enumerator that allows foreach to be used to process the parameter values in this collection. |
Remarks
Admin, 8/24/2017.
GetPropertiesWithAttribute<T>(Type)
Gets the properties with attributes in this collection.
Declaration
public static IEnumerable<PropertyInfo> GetPropertiesWithAttribute<T>(this Type type)
where T : Attribute
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type. |
Returns
Type | Description |
---|---|
IEnumerable<PropertyInfo> | An enumerator that allows foreach to be used to process the properties with attributes in this collection. |
Type Parameters
Name | Description |
---|---|
T | Generic type parameter. |
Remarks
Admin, 8/24/2017.
GetPropertySelector<T>(PropertyInfo)
A PropertyInfo extension method that gets property selector.
Declaration
public static Expression<Func<T, object>> GetPropertySelector<T>(this PropertyInfo propInfo)
Parameters
Type | Name | Description |
---|---|---|
PropertyInfo | propInfo | The propInfo to act on. |
Returns
Type | Description |
---|---|
Expression<Func<T, System.Object>> | The property selector. |
Type Parameters
Name | Description |
---|---|
T | Generic type parameter. |
Remarks
Admin, 8/24/2017.
GetPropertyType(Type, String)
Gets the type of the property by name.
Declaration
public static Type GetPropertyType(this Type type, string propertyName)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type. |
System.String | propertyName | Name of the property. |
Returns
Type | Description |
---|---|
Type | System.Type. |
Remarks
Admin, 8/24/2017.
GetTypeByName(String)
Returns the type based on the full name.
Declaration
public static Type GetTypeByName(string fullName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fullName | The full name. |
Returns
Type | Description |
---|---|
Type | Type. |
Remarks
Admin, 8/24/2017.
GetValueOrDefault<TK, TV>(IDictionary<TK, TV>, TK)
Returns the instance of the element, or the default value if the element does not exist.
Declaration
public static object GetValueOrDefault<TK, TV>(this IDictionary<TK, TV> dict, TK key)
where TV : class
Parameters
Type | Name | Description |
---|---|---|
IDictionary<TK, TV> | dict | The dictionary. |
TK | key | The key. |
Returns
Type | Description |
---|---|
System.Object | System.Object. |
Type Parameters
Name | Description |
---|---|
TK | The type of the key. |
TV | The type of the value. |
Remarks
Admin, 8/24/2017.
GetVersionFromAssemblyFullName(String)
Gets version from assembly full name.
Declaration
public static Version GetVersionFromAssemblyFullName(string fullName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fullName | . |
Returns
Type | Description |
---|---|
Version | The version from assembly full name. |
Remarks
Admin, 8/24/2017.
HasAttribute<TAttributeType>(PropertyInfo, Boolean)
Returns true if the specified attribute type is attached to the property.
Declaration
public static bool HasAttribute<TAttributeType>(this PropertyInfo property, bool inherit = true)
where TAttributeType : Attribute
Parameters
Type | Name | Description |
---|---|---|
PropertyInfo | property | . |
System.Boolean | inherit | (Optional) |
Returns
Type | Description |
---|---|
System.Boolean | True if attribute, false if not. |
Type Parameters
Name | Description |
---|---|
TAttributeType | . |
Remarks
Admin, 8/24/2017.
HasAttribute<TAttributeType>(Type, Boolean)
Returns true if the specified attribute type is attached to the type.
Declaration
public static bool HasAttribute<TAttributeType>(this Type type, bool inherit = true)
where TAttributeType : Attribute
Parameters
Type | Name | Description |
---|---|---|
Type | type | . |
System.Boolean | inherit | (Optional) |
Returns
Type | Description |
---|---|
System.Boolean | True if attribute, false if not. |
Type Parameters
Name | Description |
---|---|
TAttributeType | . |
Remarks
Admin, 8/24/2017.
HasType(Assembly, String)
Tests if the assembly contains the specified type name.
Declaration
public static bool HasType(this Assembly assembly, string typeName)
Parameters
Type | Name | Description |
---|---|---|
Assembly | assembly | . |
System.String | typeName | Type name to check for, must be qualified with namespace. |
Returns
Type | Description |
---|---|
System.Boolean | True if type, false if not. |
Remarks
Admin, 8/24/2017.
InvokeToJsonAsync(MethodInfo, Object, String)
A MethodInfo extension method that executes to JSON asynchronous on a different thread, and waits for the result.
Declaration
public static Task<string> InvokeToJsonAsync(this MethodInfo method, object instance, string parameters = "")
Parameters
Type | Name | Description |
---|---|---|
MethodInfo | method | The method to act on. |
System.Object | instance | The instance. |
System.String | parameters | (Optional) Options for controlling the operation. |
Returns
Type | Description |
---|---|
Task<System.String> | An asynchronous result that yields the invoke to JSON. |
Remarks
Admin, 8/24/2017.
IsAssemblyLoaded(System.AppDomain, String)
Returns true if the assembly has been loaded into the appdomain.
Declaration
public static bool IsAssemblyLoaded(this System.AppDomain domain, string assemblyFullName)
Parameters
Type | Name | Description |
---|---|---|
System.AppDomain | domain | . |
System.String | assemblyFullName | . |
Returns
Type | Description |
---|---|
System.Boolean | True if assembly loaded, false if not. |
Remarks
Admin, 8/24/2017.
IsCollectionProperty(PropertyInfo)
Determines whether the specified property is a collection.
Declaration
public static bool IsCollectionProperty(this PropertyInfo propertyInfo)
Parameters
Type | Name | Description |
---|---|---|
PropertyInfo | propertyInfo | The property information. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
Admin, 8/24/2017.
IsCollectionProperty(Type)
Checks if the type represents a collection.
Declaration
public static bool IsCollectionProperty(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
Admin, 8/24/2017.
IsNonStringEnumerable(Type)
Returns true if the specified type is not a string, but is IEnumerable.
Declaration
public static bool IsNonStringEnumerable(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | . |
Returns
Type | Description |
---|---|
System.Boolean | True if non string enumerable, false if not. |
Remarks
Admin, 8/24/2017.
LoadLocalFileAssemblies()
Loads all dlls that are in the local process path.
Declaration
public static void LoadLocalFileAssemblies()
Remarks
Admin, 8/24/2017.
MatchAssemblyName(String, String)
We sometimes need to match an assembly name based on a partial name in order to attempt to load the assembly, this helper method encapsulates the logic to test the various permutations.
Declaration
public static bool MatchAssemblyName(string name1, string name2)
Parameters
Type | Name | Description |
---|---|---|
System.String | name1 | . |
System.String | name2 | . |
Returns
Type | Description |
---|---|
System.Boolean | True if it succeeds, false if it fails. |
Remarks
Admin, 8/24/2017.
ResolveAllOpenGenericTypes(Type)
This will return a type definition for all inherited generic types from the given type. It only works on single constrained generic types.
Declaration
public static IEnumerable<Type> ResolveAllOpenGenericTypes(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | . |
Returns
Type | Description |
---|---|
IEnumerable<Type> | A list of Types that can be resolved that inherit from the specified type. |
Remarks
Admin, 8/24/2017.
ResolveAllOpenGenericTypes<TBaseType>()
This will return a type definition for all inherited generic types from the given type. It only works on single constrained generic types.
Declaration
public static IEnumerable<Type> ResolveAllOpenGenericTypes<TBaseType>()
Returns
Type | Description |
---|---|
IEnumerable<Type> | IEnumerable<Type>. |
Type Parameters
Name | Description |
---|---|
TBaseType | The type of the t base type. |
Remarks
Admin, 8/24/2017.
ToArray(IEnumerable, Type)
Helper method to convert an IEnumerable to an array without using a generic argument.
Declaration
public static Array ToArray(this IEnumerable source, Type type)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable | source | Enumerable source. |
Type | type | Desired array type. |
Returns
Type | Description |
---|---|
Array | An array that represents the data in this object. |
Remarks
Admin, 8/24/2017.
UnWrapDynamicProxyType(Type)
Unwraps the dynamic proxy, and returns the base entity type.
Declaration
public static Type UnWrapDynamicProxyType(this Type entityType)
Parameters
Type | Name | Description |
---|---|---|
Type | entityType | Type of the entity. |
Returns
Type | Description |
---|---|
Type | Type. |
Remarks
Admin, 8/24/2017.