Class ReflectionUtilities
Provides methods for working with class and type information at runtime.
Inheritance
Inherited Members
Namespace: Terrasoft.Common
Assembly: Terrasoft.Common.dll
Syntax
public static class ReflectionUtilities
Methods
ApplyPropertyAction(Object, Type, Action<Object, PropertyInfo>)
Applies action for each property with given attribute.
Declaration
public static void ApplyPropertyAction(object source, Type attributeType, Action<object, PropertyInfo> action)
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | Object. |
System.Type | attributeType | Type of attribute to find. |
System.Action<System.Object, System.Reflection.PropertyInfo> | action | Action to execute for found properties. |
FindTypeByShortName(Assembly, String)
Searches type by specified short name in the specified assembly.
Declaration
public static Type FindTypeByShortName(Assembly assembly, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.Assembly | assembly | Assembly to search in. |
System.String | name | The name of the type. |
Returns
Type | Description |
---|---|
System.Type |
|
ForceGetValue(PropertyInfo, Object, Object[])
Gets the current property value or value of the get_source
property name of the specified obj
object.
Declaration
public static object ForceGetValue(this PropertyInfo source, object obj, object[] args)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.PropertyInfo | source | Attributes and meta data of the current property. |
System.Object | obj | Object whose value is returned if the |
System.Object[] | args |
Returns
Type | Description |
---|---|
System.Object | Object that contains the property value. |
Remarks
The extension method for the System.Reflection.PropertyInfo class.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
InvalidObjectStateException | If the |
GetDefValue(PropertyInfo)
Gets the property value by default for the specified property.
Declaration
public static object GetDefValue(PropertyInfo property)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.PropertyInfo | property | Property. |
Returns
Type | Description |
---|---|
System.Object | Default value object for the |
GetDefValue(Type)
Gets the property value by default for the specified value type.
Declaration
public static object GetDefValue(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | Value type. |
Returns
Type | Description |
---|---|
System.Object | Default value object for the specified value type |
GetHierarchy(Type, Type)
Gets hierarchy of types.
Declaration
public static List<Type> GetHierarchy(this Type source, Type to)
Parameters
Type | Name | Description |
---|---|---|
System.Type | source | Type from to check. |
System.Type | to | Type to check to. |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<System.Type> | Hierarchy types. |
GetInstanceFactory<TInstance>(Type)
Returns delegate that create instance of specified type using public parameterless constructor.
Declaration
public static Func<TInstance> GetInstanceFactory<TInstance>(Type instanceType)
Parameters
Type | Name | Description |
---|---|---|
System.Type | instanceType | Type of the instance. |
Returns
Type | Description |
---|---|
System.Func<TInstance> | Delegate that create instance of specified type if type has public parameterless
constructor, otherwise |
Type Parameters
Name | Description |
---|---|
TInstance | The type of the instance. |
GetIsOverriden(Type, MethodInfo)
Gets the value indicating whether method have been overriden in the source type.
Declaration
public static bool GetIsOverriden(this Type source, MethodInfo methodInfo)
Parameters
Type | Name | Description |
---|---|---|
System.Type | source | The property access expression. |
System.Reflection.MethodInfo | methodInfo | Method to check. |
Returns
Type | Description |
---|---|
System.Boolean | Property info instance. |
GetMethodInfo(Type, String, BindingFlags)
Declaration
public static MethodInfo GetMethodInfo(Type type, string methodName, BindingFlags bindingFlags)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | |
System.String | methodName | |
System.Reflection.BindingFlags | bindingFlags |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo |
GetPrivateFieldValue(Object, String)
Returns the value of a private field.
Declaration
public static object GetPrivateFieldValue(object instance, string fieldName)
Parameters
Type | Name | Description |
---|---|---|
System.Object | instance | Instance. |
System.String | fieldName | Field name. |
Returns
Type | Description |
---|---|
System.Object |
GetPrivateFieldValue(Object, Type, String)
Returns the value of a private field.
Declaration
public static object GetPrivateFieldValue(object instance, Type type, string fieldName)
Parameters
Type | Name | Description |
---|---|---|
System.Object | instance | Instance. |
System.Type | type | Type. |
System.String | fieldName | Field name. |
Returns
Type | Description |
---|---|
System.Object |
GetPrivatePropertyValue(Object, Type, String)
Returns the value of a private property.
Declaration
public static object GetPrivatePropertyValue(object instance, Type type, string propertyName)
Parameters
Type | Name | Description |
---|---|---|
System.Object | instance | Instance. |
System.Type | type | Type. |
System.String | propertyName | Property name. |
Returns
Type | Description |
---|---|
System.Object |
GetPropertyDefValue(Object, String, Object)
Gets property value with the specified name or default value if the current type does not contain a property with such name.
Declaration
public static object GetPropertyDefValue(this object source, string propertyName, object defValue)
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | Type. |
System.String | propertyName | Property name. |
System.Object | defValue | The default value. |
Returns
Type | Description |
---|---|
System.Object | Object that contains the property value. |
Exceptions
Type | Condition |
---|---|
ArgumentNullOrEmptyException | If the empty value or |
InvalidObjectStateException | If the |
GetPropertyInfo<TSource, TValue>(Expression<Func<TSource, TValue>>)
Gets the expression property name.
Declaration
public static PropertyInfo GetPropertyInfo<TSource, TValue>(this Expression<Func<TSource, TValue>> source)
Parameters
Type | Name | Description |
---|---|---|
System.Linq.Expressions.Expression<System.Func<TSource, TValue>> | source | The property access expression. |
Returns
Type | Description |
---|---|
System.Reflection.PropertyInfo | Property info instance. |
Type Parameters
Name | Description |
---|---|
TSource | The property owner type. |
TValue | The property type. |
GetPropertyValue(Object, String)
Gets property value with the specified name for the instance of the current type.
Declaration
public static object GetPropertyValue(this object source, string propertyName)
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | Type. |
System.String | propertyName | Property name. |
Returns
Type | Description |
---|---|
System.Object | Object that contains the property value. |
Remarks
The extension method for the System.Object class.
Exceptions
Type | Condition |
---|---|
ArgumentNullOrEmptyException | If the empty value or |
InvalidObjectStateException | If the instance of the current type does not contain a property with the name |
GetPropertyValue(Object, String, BindingFlags)
Gets property value with the specified name or throws an exception if the current instance does not contain a property with such name.
Declaration
public static object GetPropertyValue(this object source, string propertyName, BindingFlags bindingFlags)
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | Type. |
System.String | propertyName | Property name. |
System.Reflection.BindingFlags | bindingFlags | Binding flags of the System.Reflection.BindingFlags type. |
Returns
Type | Description |
---|---|
System.Object | The value of the |
GetPropertyValueByPath(Object, String)
Gets property value by the specified property path for the instance of the current type.
Declaration
public static object GetPropertyValueByPath(this object source, string propertyPath)
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | Type. |
System.String | propertyPath | Path to the property in the following format: [Property name].[Nested property name].<...>.[Resulting property name]. |
Returns
Type | Description |
---|---|
System.Object | The value of the |
Remarks
The extension method for the System.Object class.
GetTypeNameWithoutAssemblyDetails(Type)
Returns the assembly qualified type name without assembly details such as version or culture.
Declaration
public static string GetTypeNameWithoutAssemblyDetails(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type. |
Returns
Type | Description |
---|---|
System.String |
HasProperty(Object, String)
Indicates if object has property by the specified name.
Declaration
public static bool HasProperty(this object source, string propertyName)
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | Object. |
System.String | propertyName | Property name. |
Returns
Type | Description |
---|---|
System.Boolean | Flag that indicates if object has property or not. |
IsTypeOf(Object, String, Boolean)
Checks if the current object is a type with specified full name.
Declaration
public static bool IsTypeOf(object obj, string typeFullName, bool shallow)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | Object to check. |
System.String | typeFullName | Full name of the type. |
System.Boolean | shallow | Indicates that the check will be performed across all hierarchies. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
Overloaded method.
IsTypeOf(Object, Type, Boolean)
Checks if the specified object is the specified type.
Declaration
public static bool IsTypeOf(object obj, Type type, bool shallow)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | Object to check. |
System.Type | type | Type - reference. |
System.Boolean | shallow | Indicates that the check will be performed across all hierarchies. |
Returns
Type | Description |
---|---|
System.Boolean |
|
SetPropertyValue(Object, String, Object)
Sets property value with the specified name for the instance of the current type.
Declaration
public static void SetPropertyValue(this object source, string propertyName, object value)
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | Type. |
System.String | propertyName | Property name. |
System.Object | value | The value of the property. |
Remarks
The extension method for the System.Object class.
Exceptions
Type | Condition |
---|---|
ArgumentNullOrEmptyException | If the empty value or |
InvalidObjectStateException | If the instance of the current type does not contain a property with the name |
InvalidTypeCastException | If the property type with the name |
SetStaticPropertyValue(Type, String, Object)
Tries to set the value of a given static property of the type.
Declaration
public static void SetStaticPropertyValue(this Type source, string propertyName, object value)
Parameters
Type | Name | Description |
---|---|---|
System.Type | source | Type. |
System.String | propertyName | Property name. |
System.Object | value | Value. |
Remarks
Extension method for System.Type.
TryGetPropertyValue(Object, String, out Object)
Determines if a property value with the specified name can be obtained from the instance of the current type.
Declaration
public static bool TryGetPropertyValue(this object source, string propertyName, out object value)
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | Type. |
System.String | propertyName | Property name. |
System.Object | value | The value of the property. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
The extension method for the System.Object class.
TrySetPropertyValue(Object, String, Object)
Attempts to set a value of the specified type for an instance of the current type.
Declaration
public static bool TrySetPropertyValue(this object source, string propertyName, object value)
Parameters
Type | Name | Description |
---|---|---|
System.Object | source | Type. |
System.String | propertyName | Property name. |
System.Object | value | The value of the property. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
The extension method for the System.Object class.