Class ReflectionUtil
Summary description for ReflectionUtil2.
Inheritance
Namespace: ImageVault.Common.Lib.Reflection
Assembly: ImageVault.Common.dll
Syntax
public static class ReflectionUtil : Object
Remarks
2011-02-16 dan: Created
Methods
ConvertValueFromNullable(Object)
Converts a value from a System.Nullable<> value
Declaration
public static object ConvertValueFromNullable(object value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | value | The value to convert |
Returns
| Type | Description |
|---|---|
| System.Object | The converted value or if the value is not a System.Nullable<>, the original value. |
DumpStackTrace()
Dumps the current stack trace
Declaration
public static string DumpStackTrace()
Returns
| Type | Description |
|---|---|
| System.String |
DumpStackTrace(String, Boolean, Func<StackFrame, Boolean>)
Gets the stacktrace from the outermost occurrence of the supplied namespace prefix
Declaration
public static string DumpStackTrace(string namespacePrefix, bool reverse = false, Func<StackFrame, bool> filter = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | namespacePrefix | |
| System.Boolean | reverse | If stack trace should be printed reverse, beginning with originating frame and ending with current frame |
| System.Func<System.Diagnostics.StackFrame, System.Boolean> | filter | Filter to use for filtering out different frames. Return true to keep frame, false to remove |
Returns
| Type | Description |
|---|---|
| System.String |
FindAllConcreteImplementations(Assembly, Type, Type[])
Iterates all types in the assembly and returns a dictionary containing of all interfaces and concrete implementations inheriting from the baseInterface
Declaration
public static Dictionary<Type, List<Type>> FindAllConcreteImplementations(Assembly assembly, Type baseInterface, params Type[] interfaceTypesToIgnore)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.Assembly | assembly | The System.Reflection.Assembly to search in |
| System.Type | baseInterface | The System.Type of the interface |
| System.Type[] | interfaceTypesToIgnore | The optional list of interfaces that should be ignored |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.Dictionary<System.Type, System.Collections.Generic.List<System.Type>> |
Remarks
Will only find interfaces that implements the baseInterface and their concrete implementations
FindAllConcreteImplementations<T>(Assembly)
Find all concrete subclasses of the supplied type
Declaration
public static IEnumerable<Type> FindAllConcreteImplementations<T>(Assembly assembly)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.Assembly | assembly | The assembly to look in |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<System.Type> |
Type Parameters
| Name | Description |
|---|---|
| T | Base type that the class must implement |
GetAttribute(MemberInfo, Type)
Gets the attribute from the supplied memberinfo that is of the supplied attribute type.
Declaration
public static Attribute GetAttribute(MemberInfo memberInfo, Type attributeType)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.MemberInfo | memberInfo | The memberinfo to get the attribute from |
| System.Type | attributeType | The type of attribute to get, if null type of attribute is irrelevant. |
Returns
| Type | Description |
|---|---|
| System.Attribute | The first attribute of the matching type |
Remarks
This will only search the supplied memberInfo, not inherited ones.
Exceptions
| Type | Condition |
|---|---|
| MemberReflectionException | If the member has more than one instance of the attribute. |
GetAttribute(MemberInfo, Type, Boolean)
Gets the attribute from the supplied memberinfo that is of the supplied attribute type.
Declaration
public static Attribute GetAttribute(MemberInfo memberInfo, Type attributeType, bool inherit)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.MemberInfo | memberInfo | The memberinfo to get the attribute from |
| System.Type | attributeType | The type of attribute to get, if null type of attribute is irrelevant. |
| System.Boolean | inherit | If the types inheritance chain should be searched for attributes |
Returns
| Type | Description |
|---|---|
| System.Attribute | The first attribute of the matching type |
Exceptions
| Type | Condition |
|---|---|
| MemberReflectionException | If the member has more than one instance of the attribute. |
GetAttribute<TAttribute>(MemberInfo)
Gets the attribute from the supplied memberinfo that is of the supplied attribute type.
Declaration
public static TAttribute GetAttribute<TAttribute>(MemberInfo memberInfo)
where TAttribute : Attribute
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.MemberInfo | memberInfo | The memberinfo to get the attribute from |
Returns
| Type | Description |
|---|---|
| TAttribute | The first attribute of the matching type |
Type Parameters
| Name | Description |
|---|---|
| TAttribute | The type of attribute to get |
GetAttribute<TAttribute>(MemberInfo, Boolean)
Gets the attribute from the supplied memberinfo that is of the supplied attribute type.
Declaration
public static TAttribute GetAttribute<TAttribute>(MemberInfo memberInfo, bool inherit)
where TAttribute : Attribute
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.MemberInfo | memberInfo | The memberinfo to get the attribute from |
| System.Boolean | inherit | If the types inheritance chain should be searched for attributes |
Returns
| Type | Description |
|---|---|
| TAttribute | The first attribute of the matching type |
Type Parameters
| Name | Description |
|---|---|
| TAttribute | The type of attribute to get |
GetAttributes(MemberInfo)
Gets the attributes from the supplied type that is of the supplied attribute type.
Declaration
public static Attribute[] GetAttributes(MemberInfo memberInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.MemberInfo | memberInfo | The memberinfo to get the attributes from |
Returns
| Type | Description |
|---|---|
| System.Attribute[] | An array of attributes |
GetAttributes(MemberInfo, Boolean)
Gets the attributes from the supplied type that is of the supplied attribute type.
Declaration
public static Attribute[] GetAttributes(MemberInfo memberInfo, bool inherit)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.MemberInfo | memberInfo | The memberinfo to get the attributes from |
| System.Boolean | inherit | If the types inheritance chain should be searched for attributes |
Returns
| Type | Description |
|---|---|
| System.Attribute[] | An array of attributes |
GetAttributes(MemberInfo, Type)
Gets the attributes from the supplied type that is of the supplied attribute type.
Declaration
public static Attribute[] GetAttributes(MemberInfo memberInfo, Type attributeType)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.MemberInfo | memberInfo | The memberinfo to get the attributes from |
| System.Type | attributeType | The type of attribute to get, if null type of attribute is irrelevant. |
Returns
| Type | Description |
|---|---|
| System.Attribute[] | An array of attributes |
Remarks
This will only search the supplied memberInfo, not inherited ones.
GetAttributes(MemberInfo, Type, Boolean)
Gets the attributes from the supplied type that is of the supplied attribute type.
Declaration
public static Attribute[] GetAttributes(MemberInfo memberInfo, Type attributeType, bool inherit)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.MemberInfo | memberInfo | The memberinfo to get the attributes from |
| System.Type | attributeType | The type of attribute to get, if null type of attribute is irrelevant. |
| System.Boolean | inherit | If the types inheritance chain should be searched for attributes |
Returns
| Type | Description |
|---|---|
| System.Attribute[] | An array of attributes |
GetAttributes<TAttribute>(MemberInfo, Boolean)
Gets the attributes from the supplied type that is of the supplied attribute type.
Declaration
public static TAttribute[] GetAttributes<TAttribute>(MemberInfo memberInfo, bool inherit)
where TAttribute : Attribute
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.MemberInfo | memberInfo | The memberinfo to get the attributes from |
| System.Boolean | inherit | If the types inheritance chain should be searched for attributes |
Returns
| Type | Description |
|---|---|
| TAttribute[] | An array of attributes |
Type Parameters
| Name | Description |
|---|---|
| TAttribute | The type of attribute to get |
GetCalleeAssembly()
Gets the assembly of the class that called the assembly of the method that calls this method. :)
Declaration
public static Assembly GetCalleeAssembly()
Returns
| Type | Description |
|---|---|
| System.Reflection.Assembly | The System.Reflection.Assembly of the method that called the assembly of the method that we call this method fromor null if the call started from the calling assembly |
GetCallerTypeMethodFullName(Type)
Gets the full name of the caller types method
Declaration
public static string GetCallerTypeMethodFullName(Type boundaryType = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | boundaryType |
Returns
| Type | Description |
|---|---|
| System.String | The methods full name on the format |
GetDefaultValue(Type)
Gets the default uninitialized value of the specific type.
Declaration
public static object GetDefaultValue(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The System.Type to get the value for |
Returns
| Type | Description |
|---|---|
| System.Object | The default value of the specified type |
Remarks
The default value is for reference types null, and for value types it's the same as the call to the default constructor.
GetDerivedTypes(Assembly, Type)
Gets all types from the assembly that is directly or indirectly derived from the supplied baseType
Declaration
public static IList<Type> GetDerivedTypes(Assembly assembly, Type baseType)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.Assembly | assembly | The assembly to search |
| System.Type | baseType | the baseType to use as a reference |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IList<System.Type> | An array of derived types in the assembly |
GetDerivedTypes(Type)
Gets all types that is directly or indirectly derived from the supplied baseType in the basetypes assembly.
Declaration
public static IList<Type> GetDerivedTypes(Type baseType)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | baseType | the baseType to use as a reference |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IList<System.Type> | An array of derived types in the assembly |
GetField(Type, String, Object)
Gets the field from the instance
Declaration
public static object GetField(Type type, string name, object instance)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The System.Type that contains the field |
| System.String | name | The name of the field |
| System.Object | instance | The instance to get the value from |
Returns
| Type | Description |
|---|---|
| System.Object | The value of the field |
GetMemberInfoType(MemberInfo)
Finds the Type of the supplied System.Reflection.MemberInfo
Declaration
public static Type GetMemberInfoType(MemberInfo info)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.MemberInfo | info | The System.Reflection.MemberInfo to get the type for (can be a System.Reflection.FieldInfo or a System.Reflection.PropertyInfo |
Returns
| Type | Description |
|---|---|
| System.Type | The found System.Type |
Exceptions
| Type | Condition |
|---|---|
| System.Exception | If cannot get the Type from the supplied memberInfo. |
GetMethodFullName()
Gets the full name of the calling method. for instance,
string ImageVault.Common.Lib.Reflection.ReflectionUtil.GetMethodFullName()
Declaration
public static string GetMethodFullName()
Returns
| Type | Description |
|---|---|
| System.String | The methods full name on the format |
GetNullableValueType(Type)
Gets the nullable type if the supplied type is a nullable
Declaration
public static Type GetNullableValueType(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The System.Type to check for nullable |
Returns
| Type | Description |
|---|---|
| System.Type | The underlying type if the supplied type is a System.Nullable<>; otherwize the supplied type is returned. |
GetOriginatingCallerTypeMethodFullName(String)
Gets the full name of the originating caller type method
Declaration
public static string GetOriginatingCallerTypeMethodFullName(string namespacePrefix)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | namespacePrefix |
Returns
| Type | Description |
|---|---|
| System.String |
GetProperty(Type, String, Object)
Gets the property from the instance
Declaration
public static object GetProperty(Type type, string name, object instance)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The System.Type that contains the property |
| System.String | name | The name of the property |
| System.Object | instance | The instance to get the value from |
Returns
| Type | Description |
|---|---|
| System.Object | The value of the property |
GetPropertyInfo(Type, String)
Gets the System.Reflection.PropertyInfo that matches the name in the type
Declaration
public static PropertyInfo GetPropertyInfo(Type type, string name)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The System.Type to search for the property in |
| System.String | name | The name of the property |
Returns
| Type | Description |
|---|---|
| System.Reflection.PropertyInfo | The found System.Reflection.PropertyInfo or null if no match was found. |
Remarks
If we find multiple properties with the same name in the object (inherited and declared with new), we will get the topmost PropertyInfo in the inheritance chain
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | If |
GetTypes(Assembly)
Gets the loadable types from the assembly
Declaration
public static Type[] GetTypes(Assembly assembly)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.Assembly | assembly | The System.Reflection.Assembly to load the types from. |
Returns
| Type | Description |
|---|---|
| System.Type[] | An System.Type array of the loadable types in the assembly. |
InvokeMember(Type, String, Object, BindingFlags, Object[])
Invokes the member
Declaration
public static object InvokeMember(Type type, string name, object instance, BindingFlags flags, params object[] parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The System.Type that contains the member |
| System.String | name | The name of the member |
| System.Object | instance | The instance to invoke on |
| System.Reflection.BindingFlags | flags | The System.Reflection.BindingFlags to use |
| System.Object[] | parameters | The System.Object array to pass as parameters |
Returns
| Type | Description |
|---|---|
| System.Object | The returnvalue |
InvokeMethod(Type, String, Object, Object[])
Invokes the method of the type.
Declaration
public static object InvokeMethod(Type type, string name, object instance, params object[] parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The System.Type that contains the method |
| System.String | name | The name of the method to invoke |
| System.Object | instance | The instance to invoke the method on |
| System.Object[] | parameters | The parameters to pass to the method |
Returns
| Type | Description |
|---|---|
| System.Object | The returnvalue from the method call |
InvokeStaticMethod(Type, String, Object[])
Invokes the static method of the type.
Declaration
public static object InvokeStaticMethod(Type type, string name, params object[] parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The System.Type that contains the method |
| System.String | name | The name of the method to invoke |
| System.Object[] | parameters | The parameters to pass to the method |
Returns
| Type | Description |
|---|---|
| System.Object | The returnvalue from the method call |
IsNonPrimitiveStruct(Type)
Checks if the supplied type is a Struct (primitives are ignored)
Declaration
public static bool IsNonPrimitiveStruct(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The type to check |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if it is struct, otherwise false. Primitives (Int32, Single etc) are not passed. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | If |
IsNullable(Type)
Checks if the supplied type is a System.Nullable<>
Declaration
public static bool IsNullable(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The System.Type to check |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if it is System.Nullable<>, otherwise false. |
LookupType(String, Type)
Looks up the typename and finds a corresponding type
Declaration
public static IList<Type> LookupType(string typeName, Type baseType)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | typeName | The name of the type to find |
| System.Type | baseType | The System.Type that the type inherits from |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IList<System.Type> | The System.Collections.Generic.IList<> of types that matches |
LookupType(String, Type, Assembly)
Looks up the typename and finds a corresponding type
Declaration
public static IList<Type> LookupType(string typeName, Type baseType, Assembly assembly)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | typeName | The name of the type to find |
| System.Type | baseType | The System.Type that the type inherits from |
| System.Reflection.Assembly | assembly | The System.Reflection.Assembly to search in |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IList<System.Type> | The System.Collections.Generic.IList<> of types that matches |
ResolvePath(Object, IEnumerable<String>)
Resolves the supplied property path from the supplied value
Declaration
public static object ResolvePath(object val, IEnumerable<string> path)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | val | |
| System.Collections.Generic.IEnumerable<System.String> | path |
Returns
| Type | Description |
|---|---|
| System.Object |
SetField(Type, String, Object, Object)
Sets the field in the instance
Declaration
public static object SetField(Type type, string name, object instance, object value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The System.Type that contains the field |
| System.String | name | The name of the field |
| System.Object | instance | The instance to set the value to |
| System.Object | value | The value to set |
Returns
| Type | Description |
|---|---|
| System.Object | null |
SetProperty(Type, String, Object, Object)
Sets the property in the instance
Declaration
public static object SetProperty(Type type, string name, object instance, object value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The System.Type that contains the property |
| System.String | name | The name of the property |
| System.Object | instance | The instance to set the value to |
| System.Object | value | The value to set |
Returns
| Type | Description |
|---|---|
| System.Object | null |