Class ReflectionExtensions
Contains a set of extensions for common reflection activities
Inherited Members
Namespace: LemonEdge.Utils
Assembly: LemonEdge.Utils.dll
Syntax
public static class ReflectionExtensions
Remarks
In general this includes methods for getting dynamic property setters and getters against unknown types. This is significantly faster than using GetValue(object) and SetValue(object, object)
Methods
ClearEventInvocations(object, string)
Set the given event eventName
for a given object obj
to null
Declaration
public static void ClearEventInvocations(this object obj, string eventName)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object that has an event you want to clean the invocation list for |
string | eventName | The name of the event you want to clear against the object instance |
GetEnumValueCustomAttributes<AttrType, EnumType>(EnumType)
Returns the apecified attribute AttrType
against an enum instance of
EnumType
Declaration
public static IEnumerable<AttrType> GetEnumValueCustomAttributes<AttrType, EnumType>(this EnumType value) where AttrType : Attribute where EnumType : Enum
Parameters
Type | Name | Description |
---|---|---|
EnumType | value | The enum value to return the specified attribute |
Returns
Type | Description |
---|---|
IEnumerable<AttrType> | An instance of the specified attribute against the specified enum instance |
Type Parameters
Name | Description |
---|---|
AttrType | The type of attribute marked against all enum values |
EnumType | The enum type to retrieve attributes from |
GetFirstOrDefaultPropertyInfo(Type, Func<PropertyInfo, bool>)
Gets the properties for the type and returns the FirstOrDefault PropertyInfo that matches the predicate.
Declaration
public static PropertyInfo? GetFirstOrDefaultPropertyInfo(this Type type, Func<PropertyInfo, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
Type | type | This type. |
Func<PropertyInfo, bool> | predicate | The predicate. |
Returns
Type | Description |
---|---|
PropertyInfo | A PropertyInfo, or null. |
GetGenericInstanceOfBaseType(Type, Type)
Given a type
this returns the type generic type that implements the
baseType
this inherits
Declaration
public static Type GetGenericInstanceOfBaseType(this Type type, Type baseType)
Parameters
Type | Name | Description |
---|---|---|
Type | type | Finds what class this class implements of the generic specified base type. This type must inherit from basetype |
Type | baseType | he type that has a generic argument(s) that you would like the whole type for |
Returns
Type | Description |
---|---|
Type | The generic type of |
Remarks
For example:
public class MyClass<T>
{
}
public class MyDecimalClass : MyClass<decimal> { }
public static Type GetMyDecimalClassType()
{
return typeof(MyDecimalClass).GetGenericInstanceOfBaseType(typeof(MyClass<>));
}
Would return the MyClass<decimal> type
GetGenericTypeArgumentOfImplementedBaseClass(Type, Type)
Given a type
that implements or inherits forGenericType
that has a generic
argument, this function will return the Type used as the generic argument for the
forGenericType
Declaration
public static Type GetGenericTypeArgumentOfImplementedBaseClass(this Type type, Type forGenericType)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type that inherits or implements |
Type | forGenericType | The type that has a generic argument that you would like the generic type for |
Returns
Type | Description |
---|---|
Type | The generic type argument for the |
Remarks
For example:
public class MyClass<T>
{
}
public class MyDecimalClass : MyClass<decimal> { }
public static Type GetMyDecimalClassType()
{
return typeof(MyDecimalClass).GetGenericTypeArgumentOfImplementedBaseClass(typeof(MyClass<>));
}
Would return the decimal type
GetNestedPropertyInfo(Type, string, string, bool)
Returns the PropertyInfo definition for the specified property against a property on this type. Given Entity.ParentProperty.NestedProperty, this returns NestedProperty's definition.
Declaration
public static PropertyInfo GetNestedPropertyInfo(this Type type, string parentPropertyName, string nestedPropertyName, bool throwIfMissing = true)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type that holds the specified property |
string | parentPropertyName | The parent property that contains nestedPropertyName |
string | nestedPropertyName | The property to return property info on |
bool | throwIfMissing | Indicates if the property isn't found an ArgumentOutOfRangeException should be thrown, otherwise null is returned |
Returns
Type | Description |
---|---|
PropertyInfo | The PropertyInfo for the specified |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | If |
GetNullableType(Type)
Returns the generic type implementation for this type used for Nullable<T> implementation
Declaration
public static Type GetNullableType(this Type t)
Parameters
Type | Name | Description |
---|---|---|
Type | t | The type that implements Nullable<T> |
Returns
Type | Description |
---|---|
Type | The generic type implementation of the Nullable<T> implementation of this type
|
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | If this type does not implement Nullable<T> then an exception is thrown |
GetPropertyInfo(Type, string, bool)
Returns the PropertyInfo definition for the specified property against this type
Declaration
public static PropertyInfo? GetPropertyInfo(this Type type, string propName, bool throwIfMissing = true)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type that holds the specified property |
string | propName | The property to return property info on |
bool | throwIfMissing | Indicates if the property isn't found an ArgumentOutOfRangeException should be thrown, otherwise null is returned |
Returns
Type | Description |
---|---|
PropertyInfo | The PropertyInfo for the specified |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException | If |
GetValue(Type, string, object)
Returns the value for the specified propertyName
againt the specified item
an instance of type type
Declaration
public static object GetValue(this Type type, string propertyName, object item)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type this property resides against that we want the value of |
string | propertyName | The name of the property we want to return the value of |
object | item | An instance of type |
Returns
Type | Description |
---|---|
object | The value of |
Remarks
This is significantly faster than using GetValue(object) and SetValue(object, object)
InheritsFromIncludingGeneric(Type, Type)
Indicates if the given type
inherits from of implements the specified
baseType
including if that type has generic arguments
Declaration
public static bool InheritsFromIncludingGeneric(this Type type, Type baseType)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type you want to know if it inherits from |
Type | baseType | The type you want to know if |
Returns
Type | Description |
---|---|
bool | True if |
InheritsFromIncludingGeneric<T>(Type)
Indicates if the given T
inherits from of implements the specified type
including if that type has generic arguments
Declaration
public static bool InheritsFromIncludingGeneric<T>(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type you want to know if |
Returns
Type | Description |
---|---|
bool | True if |
Type Parameters
Name | Description |
---|---|
T | The type you want to know if it inherits from |
InheritsFromType(Type, string)
Returns true if the specified type type
inherits from a class called
typeName
Declaration
public static bool InheritsFromType(this Type type, string typeName)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type to see if it inherits any class called |
string | typeName | The name of a type you want to see if the type |
Returns
Type | Description |
---|---|
bool |
IsNullable(Type)
Indicates if this type if nullable, as in is a type of Nullable<T>
Declaration
public static bool IsNullable(this Type t)
Parameters
Type | Name | Description |
---|---|---|
Type | t | The type to check to see if it is nullable or not |
Returns
Type | Description |
---|---|
bool | True if this type is of the type Nullable<T> |
SetValue(Type, string, object, object, bool)
Set the value for the specified propertyName
to value
againt the specified
item
an instance of type type
Declaration
public static void SetValue(this Type type, string propertyName, object item, object value, bool throwIfPropertyMissing = true)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type this property resides against that we want the value of |
string | propertyName | The name of the property we want to return the value of |
object | item | An instance of type |
object | value | The value to set for the |
bool | throwIfPropertyMissing |
Remarks
This is significantly faster than using GetValue(object) and SetValue(object, object)
ToCodeTypeName(Type)
Used for auto code generation. Returns the name of a given type in c# style syntax
Declaration
public static string ToCodeTypeName(this Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type | The type to return a c# formatted type code for |
Returns
Type | Description |
---|---|
string | The specified type in c# style coding |
Remarks
For example List<int> would be returned as List<int> and not List'1Int
ToPropertyAccessorExpression<T, R>(PropertyInfo, int?, bool)
Returns a expression (for use with Linq and other technologies) of a function that given an instance of
T
will return the value of property p
as type R
Declaration
public static Expression<Func<T, R>> ToPropertyAccessorExpression<T, R>(this PropertyInfo p, int? index = null, bool cast = true)
Parameters
Type | Name | Description |
---|---|---|
PropertyInfo | p | The property to return a value for |
int? | index | If the property has an indexer this holds the index value to the property |
bool | cast | Indicates if the value being returned should be cast to |
Returns
Type | Description |
---|---|
Expression<Func<T, R>> | An Expression<TDelegate> (for use with Linq and other technologies) of
Func<T, TResult> that takes an instance of the object and returns the value for the specified
|
Type Parameters
Name | Description |
---|---|
T | The type of the item that will be passed to return the property value of |
R | The return type from the property |
Remarks
This is significantly faster than using GetValue(object) and SetValue(object, object)
ToPropertyAccessor<T>(PropertyInfo, int?)
Returns a function that given an instance of T
will return the value of property
p
Declaration
public static Func<T, object> ToPropertyAccessor<T>(this PropertyInfo p, int? index = null)
Parameters
Type | Name | Description |
---|---|---|
PropertyInfo | p | The property to return a value for |
int? | index | If the property has an indexer this holds the index value to the property |
Returns
Type | Description |
---|---|
Func<T, object> | An Func<T, TResult> that takes an instance of the object and returns the value for the
specified |
Type Parameters
Name | Description |
---|---|
T | The type of the item that will be passed to return the property value of |
Remarks
This is significantly faster than using GetValue(object) and SetValue(object, object)
ToPropertyAccessor<T>(PropertyInfo, Type, int?)
Returns a function that given an instance of T
will return the value of property
p
Declaration
public static Func<T, object> ToPropertyAccessor<T>(this PropertyInfo p, Type actualTypeThatImplementsT, int? index = null)
Parameters
Type | Name | Description |
---|---|---|
PropertyInfo | p | The property to return a value for |
Type | actualTypeThatImplementsT | The actual type that implements the property |
int? | index | If the property has an indexer this holds the index value to the property |
Returns
Type | Description |
---|---|
Func<T, object> | An Func<T, TResult> that takes an instance of the object and returns the value for the
specified |
Type Parameters
Name | Description |
---|---|
T | The type of the item that will be passed to return the property value of |
Remarks
This is significantly faster than using GetValue(object) and SetValue(object, object)
ToPropertyAccessor<T, R>(PropertyInfo, int?)
Returns a function that given an instance of T
will return the value of property
p
as type R
Declaration
public static Func<T, R> ToPropertyAccessor<T, R>(this PropertyInfo p, int? index = null)
Parameters
Type | Name | Description |
---|---|---|
PropertyInfo | p | The property to return a value for |
int? | index | If the property has an indexer this holds the index value to the property |
Returns
Type | Description |
---|---|
Func<T, R> | An Func<T, TResult> that takes an instance of the object and returns the value for the
specified |
Type Parameters
Name | Description |
---|---|
T | The type of the item that will be passed to return the property value of |
R | The return type from the property |
Remarks
This is significantly faster than using GetValue(object) and SetValue(object, object)
ToPropertySetter<T>(PropertyInfo, int?)
Returns an action that can be used to set the property value for the specified propInfo
against
an instance of the type
Declaration
public static Action<T, object> ToPropertySetter<T>(this PropertyInfo propInfo, int? index = null)
Parameters
Type | Name | Description |
---|---|---|
PropertyInfo | propInfo | The propertyinfo which describes the property you want to be able to set the value against |
int? | index | If the property has an indexer this holds the index value to the property |
Returns
Type | Description |
---|---|
Action<T, object> | An Action<T1, T2> that takes an instance of the object and the value to set the specified
|
Type Parameters
Name | Description |
---|---|
T | The type of object that you want to set a property value against |
Remarks
This is significantly faster than using GetValue(object) and SetValue(object, object)