Class ReflectionExtensions
Contains a set of extensions for common reflection activities
Inheritance
Inherited Members
Namespace: LemonEdge.Utils
Assembly: 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 System.Reflection.PropertyInfo.GetValue(System.Object) and System.Reflection.PropertyInfo.SetValue(System.Object,System.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 |
|---|---|---|
| System.Object | obj | The object that has an event you want to clean the invocation list for |
| System.String | eventName | The name of the event you want to clear against the object instance |
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 |
|---|---|---|
| System.Type | type | Finds what class this class implements of the generic specified base type. This type must inherit from basetype |
| System.Type | baseType | he type that has a generic argument(s) that you would like the whole type for |
Returns
| Type | Description |
|---|---|
| System.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 |
|---|---|---|
| System.Type | type | The type that inherits or implements |
| System.Type | forGenericType | The type that has a generic argument that you would like the generic type for |
Returns
| Type | Description |
|---|---|
| System.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
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 |
|---|---|---|
| System.Type | type | The type this property resides against that we want the value of |
| System.String | propertyName | The name of the property we want to return the value of |
| System.Object | item | An instance of type |
Returns
| Type | Description |
|---|---|
| System.Object | The value of |
Remarks
This is significantly faster than using System.Reflection.PropertyInfo.GetValue(System.Object) and System.Reflection.PropertyInfo.SetValue(System.Object,System.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 |
|---|---|---|
| System.Type | type | The type you want to know if it inherits from |
| System.Type | baseType | The type you want to know if |
Returns
| Type | Description |
|---|---|
| System.Boolean | 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 |
|---|---|---|
| System.Type | type | The type you want to know if |
Returns
| Type | Description |
|---|---|
| System.Boolean | 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 |
|---|---|---|
| System.Type | type | The type to see if it inherits any class called |
| System.String | typeName | The name of a type you want to see if the type |
Returns
| Type | Description |
|---|---|
| System.Boolean |
SetValue(Type, String, Object, Object)
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)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Type | type | The type this property resides against that we want the value of |
| System.String | propertyName | The name of the property we want to return the value of |
| System.Object | item | An instance of type |
| System.Object | value | The value to set for the |
Remarks
This is significantly faster than using System.Reflection.PropertyInfo.GetValue(System.Object) and System.Reflection.PropertyInfo.SetValue(System.Object,System.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 |
|---|---|---|
| System.Type | type | The type to return a c# formatted type code for |
Returns
| Type | Description |
|---|---|
| System.String | The specified type in c# style coding |
Remarks
For example List<int> would be returned as List<int> and not List'1Int
ToPropertyAccessor<T>(PropertyInfo, Nullable<Int32>)
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 = default(int? ))
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.PropertyInfo | p | The property to return a value for |
| System.Nullable<System.Int32> | index | If the property has an indexer this holds the index value to the property |
Returns
| Type | Description |
|---|---|
| System.Func<T, System.Object> | An System.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 System.Reflection.PropertyInfo.GetValue(System.Object) and System.Reflection.PropertyInfo.SetValue(System.Object,System.Object)
ToPropertyAccessor<T>(PropertyInfo, Type, Nullable<Int32>)
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 = default(int? ))
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.PropertyInfo | p | The property to return a value for |
| System.Type | actualTypeThatImplementsT | The actual type that implements the property |
| System.Nullable<System.Int32> | index | If the property has an indexer this holds the index value to the property |
Returns
| Type | Description |
|---|---|
| System.Func<T, System.Object> | An System.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 System.Reflection.PropertyInfo.GetValue(System.Object) and System.Reflection.PropertyInfo.SetValue(System.Object,System.Object)
ToPropertyAccessor<T, R>(PropertyInfo, Nullable<Int32>)
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 = default(int? ))
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.PropertyInfo | p | The property to return a value for |
| System.Nullable<System.Int32> | index | If the property has an indexer this holds the index value to the property |
Returns
| Type | Description |
|---|---|
| System.Func<T, R> | An System.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 System.Reflection.PropertyInfo.GetValue(System.Object) and System.Reflection.PropertyInfo.SetValue(System.Object,System.Object)
ToPropertyAccessorExpression<T, R>(PropertyInfo, Nullable<Int32>, Boolean)
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 = default(int? ), bool cast = true)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.PropertyInfo | p | The property to return a value for |
| System.Nullable<System.Int32> | index | If the property has an indexer this holds the index value to the property |
| System.Boolean | cast | Indicates if the value being returned should be cast to |
Returns
| Type | Description |
|---|---|
| System.Linq.Expressions.Expression<System.Func<T, R>> | An System.Linq.Expressions.Expression<TDelegate> (for use with Linq and other technologies) of System.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 System.Reflection.PropertyInfo.GetValue(System.Object) and System.Reflection.PropertyInfo.SetValue(System.Object,System.Object)
ToPropertySetter<T>(PropertyInfo, Nullable<Int32>)
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 = default(int? ))
Parameters
| Type | Name | Description |
|---|---|---|
| System.Reflection.PropertyInfo | propInfo | The propertyinfo which describes the property you want to be able to set the value against |
| System.Nullable<System.Int32> | index | If the property has an indexer this holds the index value to the property |
Returns
| Type | Description |
|---|---|
| System.Action<T, System.Object> | An System.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 System.Reflection.PropertyInfo.GetValue(System.Object) and System.Reflection.PropertyInfo.SetValue(System.Object,System.Object)