Class ReflectionUtilities
Provides utilities to simplify reflection.
Inheritance
Namespace: Umbraco.Cms.Core
Assembly: Umbraco.Core.dll
Syntax
public static class ReflectionUtilities
Remarks
Readings: * CIL instructions: https://en.wikipedia.org/wiki/List_of_CIL_instructions * ECMA 335: https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf * MSIL programming: http://www.blackbeltcoder.com/Articles/net/msil-programming-part-1
Supports emitting constructors, instance and static methods, instance property getters and setters. Does not support static properties yet.
Methods
View SourceEmitConstructorUnsafe<TLambda>(ConstructorInfo)
Emits a constructor.
Declaration
public static TLambda EmitConstructorUnsafe<TLambda>(ConstructorInfo ctor)
Parameters
| Type | Name | Description |
|---|---|---|
| ConstructorInfo | ctor | The constructor info. |
Returns
| Type | Description |
|---|---|
| TLambda | A constructor function. |
Type Parameters
| Name | Description |
|---|---|
| TLambda | A lambda representing the constructor. |
Remarks
The constructor is emitted in an unsafe way, using the lambda arguments without verifying them at all. This assumes that the calling code is taking care of all verifications, in order to avoid cast errors.
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Occurs when |
| ArgumentNullException | Occurs when |
EmitConstructor<TLambda>(bool, Type?)
Emits a constructor.
Declaration
public static TLambda? EmitConstructor<TLambda>(bool mustExist = true, Type? declaring = null)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | mustExist | A value indicating whether the constructor must exist. |
| Type | declaring | The optional type of the class to construct. |
Returns
| Type | Description |
|---|---|
| TLambda | A constructor function. If |
Type Parameters
| Name | Description |
|---|---|
| TLambda | A lambda representing the constructor. |
Remarks
When declaring is not specified, it is the type returned by
TLambda.
The constructor arguments are determined by TLambda generic arguments.
The type returned by TLambda does not need to be exactly declaring,
when e.g. that type is not known at compile time, but it has to be a parent type (eg an interface, or
object).
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Occurs when the constructor does not exist and
|
| ArgumentException | Occurs when |
EmitConstructor<TLambda>(ConstructorInfo)
Emits a constructor.
Declaration
public static TLambda EmitConstructor<TLambda>(ConstructorInfo ctor)
Parameters
| Type | Name | Description |
|---|---|---|
| ConstructorInfo | ctor | The constructor info. |
Returns
| Type | Description |
|---|---|
| TLambda | A constructor function. |
Type Parameters
| Name | Description |
|---|---|
| TLambda | A lambda representing the constructor. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Occurs when |
| ArgumentNullException | Occurs when |
EmitFieldGetterAndSetter<TDeclaring, TValue>(string)
Emits a field getter and setter.
Declaration
public static (Func<TDeclaring, TValue>, Action<TDeclaring, TValue>) EmitFieldGetterAndSetter<TDeclaring, TValue>(string fieldName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | fieldName | The name of the field. |
Returns
| Type | Description |
|---|---|
| (Func<TDeclaring, TValue>, Action<TDeclaring, TValue>) | A field getter and setter functions. |
Type Parameters
| Name | Description |
|---|---|
| TDeclaring | The declaring type. |
| TValue | The field type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | fieldName |
| ArgumentException | Value can't be empty or consist only of white-space characters. - |
| InvalidOperationException | Could not find field |
EmitFieldGetter<TDeclaring, TValue>(string)
Emits a field getter.
Declaration
public static Func<TDeclaring, TValue> EmitFieldGetter<TDeclaring, TValue>(string fieldName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | fieldName | The name of the field. |
Returns
| Type | Description |
|---|---|
| Func<TDeclaring, TValue> | A field getter function. |
Type Parameters
| Name | Description |
|---|---|
| TDeclaring | The declaring type. |
| TValue | The field type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | fieldName |
| ArgumentException | Value can't be empty or consist only of white-space characters. - |
| InvalidOperationException | Could not find field |
EmitFieldSetter<TDeclaring, TValue>(string)
Emits a field setter.
Declaration
public static Action<TDeclaring, TValue> EmitFieldSetter<TDeclaring, TValue>(string fieldName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | fieldName | The name of the field. |
Returns
| Type | Description |
|---|---|
| Action<TDeclaring, TValue> | A field setter action. |
Type Parameters
| Name | Description |
|---|---|
| TDeclaring | The declaring type. |
| TValue | The field type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | fieldName |
| ArgumentException | Value can't be empty or consist only of white-space characters. - |
| InvalidOperationException | Could not find field |
EmitMethodUnsafe<TLambda>(MethodInfo)
Emits a method.
Declaration
public static TLambda EmitMethodUnsafe<TLambda>(MethodInfo method)
Parameters
| Type | Name | Description |
|---|---|---|
| MethodInfo | method | The method info. |
Returns
| Type | Description |
|---|---|
| TLambda | The method. |
Type Parameters
| Name | Description |
|---|---|
| TLambda | A lambda representing the method. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Occurs when |
| ArgumentException | Occurs when Occurs when |
EmitMethod<TLambda>(MethodInfo)
Emits a method.
Declaration
public static TLambda EmitMethod<TLambda>(MethodInfo method)
Parameters
| Type | Name | Description |
|---|---|---|
| MethodInfo | method | The method info. |
Returns
| Type | Description |
|---|---|
| TLambda | The method. |
Type Parameters
| Name | Description |
|---|---|
| TLambda | A lambda representing the method. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Occurs when |
| ArgumentException | Occurs when Occurs when |
EmitMethod<TLambda>(string, bool)
Emits an instance method.
Declaration
public static TLambda? EmitMethod<TLambda>(string methodName, bool mustExist = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | methodName | The name of the method. |
| bool | mustExist | A value indicating whether the constructor must exist. |
Returns
| Type | Description |
|---|---|
| TLambda | The method. If |
Type Parameters
| Name | Description |
|---|---|
| TLambda | A lambda representing the method. |
Remarks
The method arguments are determined by TLambda generic arguments.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | methodName |
| ArgumentException | Value can't be empty or consist only of white-space characters. - |
| InvalidOperationException | Occurs when no proper method with name |
EmitMethod<TLambda>(Type, string, bool)
Emits a static method.
Declaration
public static TLambda? EmitMethod<TLambda>(Type declaring, string methodName, bool mustExist = true)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | declaring | The declaring type. |
| string | methodName | The name of the method. |
| bool | mustExist | A value indicating whether the constructor must exist. |
Returns
| Type | Description |
|---|---|
| TLambda | The method. If |
Type Parameters
| Name | Description |
|---|---|
| TLambda | A lambda representing the method. |
Remarks
The method arguments are determined by TLambda generic arguments.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | methodName |
| ArgumentException | Value can't be empty or consist only of white-space characters. - |
| InvalidOperationException | Occurs when no proper method with name |
EmitMethod<TDeclaring, TLambda>(string, bool)
Emits a static method.
Declaration
public static TLambda? EmitMethod<TDeclaring, TLambda>(string methodName, bool mustExist = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | methodName | The name of the method. |
| bool | mustExist | A value indicating whether the constructor must exist. |
Returns
| Type | Description |
|---|---|
| TLambda | The method. If |
Type Parameters
| Name | Description |
|---|---|
| TDeclaring | The declaring type. |
| TLambda | A lambda representing the method. |
Remarks
The method arguments are determined by TLambda generic arguments.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | methodName |
| ArgumentException | Value can't be empty or consist only of white-space characters. - |
| InvalidOperationException | Occurs when no proper method with name |
EmitPropertyGetterAndSetter<TDeclaring, TValue>(PropertyInfo)
Emits a property getter and setter.
Declaration
public static (Func<TDeclaring, TValue>, Action<TDeclaring, TValue>) EmitPropertyGetterAndSetter<TDeclaring, TValue>(PropertyInfo propertyInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| PropertyInfo | propertyInfo | The property info. |
Returns
| Type | Description |
|---|---|
| (Func<TDeclaring, TValue>, Action<TDeclaring, TValue>) | A property getter and setter functions. |
Type Parameters
| Name | Description |
|---|---|
| TDeclaring | The declaring type. |
| TValue | The property type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Occurs when |
| ArgumentException | Occurs when the property has no getter or no setter. |
| ArgumentException | Occurs when |
EmitPropertyGetterAndSetter<TDeclaring, TValue>(string, bool)
Emits a property getter and setter.
Declaration
public static (Func<TDeclaring, TValue>, Action<TDeclaring, TValue>) EmitPropertyGetterAndSetter<TDeclaring, TValue>(string propertyName, bool mustExist = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | propertyName | The name of the property. |
| bool | mustExist | A value indicating whether the property and its getter and setter must exist. |
Returns
| Type | Description |
|---|---|
| (Func<TDeclaring, TValue>, Action<TDeclaring, TValue>) | A property getter and setter functions. If |
Type Parameters
| Name | Description |
|---|---|
| TDeclaring | The declaring type. |
| TValue | The property type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | propertyName |
| ArgumentException | Value can't be empty or consist only of white-space characters. - |
| InvalidOperationException | Could not find property getter and setter for
|
EmitPropertyGetter<TDeclaring, TValue>(PropertyInfo)
Emits a property getter.
Declaration
public static Func<TDeclaring, TValue> EmitPropertyGetter<TDeclaring, TValue>(PropertyInfo propertyInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| PropertyInfo | propertyInfo | The property info. |
Returns
| Type | Description |
|---|---|
| Func<TDeclaring, TValue> | A property getter function. |
Type Parameters
| Name | Description |
|---|---|
| TDeclaring | The declaring type. |
| TValue | The property type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Occurs when |
| ArgumentException | Occurs when the property has no getter. |
| ArgumentException | Occurs when |
EmitPropertyGetter<TDeclaring, TValue>(string, bool)
Emits a property getter.
Declaration
public static Func<TDeclaring, TValue>? EmitPropertyGetter<TDeclaring, TValue>(string propertyName, bool mustExist = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | propertyName | The name of the property. |
| bool | mustExist | A value indicating whether the property and its getter must exist. |
Returns
| Type | Description |
|---|---|
| Func<TDeclaring, TValue> | A property getter function. If |
Type Parameters
| Name | Description |
|---|---|
| TDeclaring | The declaring type. |
| TValue | The property type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | propertyName |
| ArgumentException | Value can't be empty or consist only of white-space characters. - |
| InvalidOperationException | Could not find property getter for |
EmitPropertySetterUnsafe<TDeclaring, TValue>(PropertyInfo)
Emits a property setter.
Declaration
public static Action<TDeclaring, TValue> EmitPropertySetterUnsafe<TDeclaring, TValue>(PropertyInfo propertyInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| PropertyInfo | propertyInfo | The property info. |
Returns
| Type | Description |
|---|---|
| Action<TDeclaring, TValue> | A property setter function. |
Type Parameters
| Name | Description |
|---|---|
| TDeclaring | The declaring type. |
| TValue | The property type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Occurs when |
| ArgumentException | Occurs when the property has no setter. |
| ArgumentException | Occurs when |
EmitPropertySetter<TDeclaring, TValue>(PropertyInfo)
Emits a property setter.
Declaration
public static Action<TDeclaring, TValue> EmitPropertySetter<TDeclaring, TValue>(PropertyInfo propertyInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| PropertyInfo | propertyInfo | The property info. |
Returns
| Type | Description |
|---|---|
| Action<TDeclaring, TValue> | A property setter function. |
Type Parameters
| Name | Description |
|---|---|
| TDeclaring | The declaring type. |
| TValue | The property type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Occurs when |
| ArgumentException | Occurs when the property has no setter. |
| ArgumentException | Occurs when |
EmitPropertySetter<TDeclaring, TValue>(string, bool)
Emits a property setter.
Declaration
public static Action<TDeclaring, TValue>? EmitPropertySetter<TDeclaring, TValue>(string propertyName, bool mustExist = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | propertyName | The name of the property. |
| bool | mustExist | A value indicating whether the property and its setter must exist. |
Returns
| Type | Description |
|---|---|
| Action<TDeclaring, TValue> | A property setter function. If |
Type Parameters
| Name | Description |
|---|---|
| TDeclaring | The declaring type. |
| TValue | The property type. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | propertyName |
| ArgumentException | Value can't be empty or consist only of white-space characters. - |
| InvalidOperationException | Could not find property setter for |