Class ReflectionUtilities
Provides utilities to simplify reflection.
Inheritance
Namespace: Umbraco.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 SourceEmitConstructor<TLambda>(Boolean, Type)
Emits a constructor.
Declaration
public static TLambda EmitConstructor<TLambda>(bool mustExist = true, Type declaring = null)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | mustExist | A value indicating whether the constructor must exist. |
System.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 |
---|---|
System.InvalidOperationException | Occurs when the constructor does not exist and |
System.ArgumentException | Occurs when |
EmitConstructor<TLambda>(ConstructorInfo)
Emits a constructor.
Declaration
public static TLambda EmitConstructor<TLambda>(ConstructorInfo ctor)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.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 |
---|---|
System.ArgumentException | Occurs when |
System.ArgumentNullException | Occurs when |
EmitConstructorUnsafe<TLambda>(ConstructorInfo)
Emits a constructor.
Declaration
public static TLambda EmitConstructorUnsafe<TLambda>(ConstructorInfo ctor)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.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 |
---|---|
System.ArgumentException | Occurs when |
System.ArgumentNullException | Occurs when |
EmitFieldGetter<TDeclaring, TValue>(String)
Emits a field getter.
Declaration
public static Func<TDeclaring, TValue> EmitFieldGetter<TDeclaring, TValue>(string fieldName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fieldName | The name of the field. |
Returns
Type | Description |
---|---|
System.Func<TDeclaring, TValue> | A field getter function. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The field type. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | fieldName |
System.ArgumentException | Value can't be empty or consist only of white-space characters. - |
System.InvalidOperationException | Could not find field |
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 |
---|---|---|
System.String | fieldName | The name of the field. |
Returns
Type | Description |
---|---|
System.ValueTuple<System.Func<TDeclaring, TValue>, System.Action<TDeclaring, TValue>> | A field getter and setter functions. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The field type. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | fieldName |
System.ArgumentException | Value can't be empty or consist only of white-space characters. - |
System.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 |
---|---|---|
System.String | fieldName | The name of the field. |
Returns
Type | Description |
---|---|
System.Action<TDeclaring, TValue> | A field setter action. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The field type. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | fieldName |
System.ArgumentException | Value can't be empty or consist only of white-space characters. - |
System.InvalidOperationException | Could not find field |
EmitMethod<TLambda>(MethodInfo)
Emits a method.
Declaration
public static TLambda EmitMethod<TLambda>(MethodInfo method)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodInfo | method | The method info. |
Returns
Type | Description |
---|---|
TLambda | The method. |
Type Parameters
Name | Description |
---|---|
TLambda | A lambda representing the method. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Occurs when |
System.ArgumentException | Occurs when Occurs when |
EmitMethod<TLambda>(String, Boolean)
Emits an instance method.
Declaration
public static TLambda EmitMethod<TLambda>(string methodName, bool mustExist = true)
Parameters
Type | Name | Description |
---|---|---|
System.String | methodName | The name of the method. |
System.Boolean | 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 |
---|---|
System.ArgumentNullException | methodName |
System.ArgumentException | Value can't be empty or consist only of white-space characters. - |
System.InvalidOperationException | Occurs when no proper method with name |
EmitMethod<TLambda>(Type, String, Boolean)
Emits a static method.
Declaration
public static TLambda EmitMethod<TLambda>(Type declaring, string methodName, bool mustExist = true)
Parameters
Type | Name | Description |
---|---|---|
System.Type | declaring | The declaring type. |
System.String | methodName | The name of the method. |
System.Boolean | 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 |
---|---|
System.ArgumentNullException | methodName |
System.ArgumentException | Value can't be empty or consist only of white-space characters. - |
System.InvalidOperationException | Occurs when no proper method with name |
EmitMethod<TDeclaring, TLambda>(String, Boolean)
Emits a static method.
Declaration
public static TLambda EmitMethod<TDeclaring, TLambda>(string methodName, bool mustExist = true)
Parameters
Type | Name | Description |
---|---|---|
System.String | methodName | The name of the method. |
System.Boolean | 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 |
---|---|
System.ArgumentNullException | methodName |
System.ArgumentException | Value can't be empty or consist only of white-space characters. - |
System.InvalidOperationException | Occurs when no proper method with name |
EmitMethodUnsafe<TLambda>(MethodInfo)
Emits a method.
Declaration
public static TLambda EmitMethodUnsafe<TLambda>(MethodInfo method)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodInfo | method | The method info. |
Returns
Type | Description |
---|---|
TLambda | The method. |
Type Parameters
Name | Description |
---|---|
TLambda | A lambda representing the method. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Occurs when |
System.ArgumentException | Occurs when Occurs when |
EmitPropertyGetter<TDeclaring, TValue>(PropertyInfo)
Emits a property getter.
Declaration
public static Func<TDeclaring, TValue> EmitPropertyGetter<TDeclaring, TValue>(PropertyInfo propertyInfo)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.PropertyInfo | propertyInfo | The property info. |
Returns
Type | Description |
---|---|
System.Func<TDeclaring, TValue> | A property getter function. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Occurs when |
System.ArgumentException | Occurs when the property has no getter. |
System.ArgumentException | Occurs when |
EmitPropertyGetter<TDeclaring, TValue>(String, Boolean)
Emits a property getter.
Declaration
public static Func<TDeclaring, TValue> EmitPropertyGetter<TDeclaring, TValue>(string propertyName, bool mustExist = true)
Parameters
Type | Name | Description |
---|---|---|
System.String | propertyName | The name of the property. |
System.Boolean | mustExist | A value indicating whether the property and its getter must exist. |
Returns
Type | Description |
---|---|
System.Func<TDeclaring, TValue> | A property getter function. If |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | propertyName |
System.ArgumentException | Value can't be empty or consist only of white-space characters. - |
System.InvalidOperationException | Could not find property getter for |
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 |
---|---|---|
System.Reflection.PropertyInfo | propertyInfo | The property info. |
Returns
Type | Description |
---|---|
System.ValueTuple<System.Func<TDeclaring, TValue>, System.Action<TDeclaring, TValue>> | A property getter and setter functions. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Occurs when |
System.ArgumentException | Occurs when the property has no getter or no setter. |
System.ArgumentException | Occurs when |
EmitPropertyGetterAndSetter<TDeclaring, TValue>(String, Boolean)
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 |
---|---|---|
System.String | propertyName | The name of the property. |
System.Boolean | mustExist | A value indicating whether the property and its getter and setter must exist. |
Returns
Type | Description |
---|---|
System.ValueTuple<System.Func<TDeclaring, TValue>, System.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 |
---|---|
System.ArgumentNullException | propertyName |
System.ArgumentException | Value can't be empty or consist only of white-space characters. - |
System.InvalidOperationException | Could not find property getter and setter for |
EmitPropertySetter<TDeclaring, TValue>(PropertyInfo)
Emits a property setter.
Declaration
public static Action<TDeclaring, TValue> EmitPropertySetter<TDeclaring, TValue>(PropertyInfo propertyInfo)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.PropertyInfo | propertyInfo | The property info. |
Returns
Type | Description |
---|---|
System.Action<TDeclaring, TValue> | A property setter function. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Occurs when |
System.ArgumentException | Occurs when the property has no setter. |
System.ArgumentException | Occurs when |
EmitPropertySetter<TDeclaring, TValue>(String, Boolean)
Emits a property setter.
Declaration
public static Action<TDeclaring, TValue> EmitPropertySetter<TDeclaring, TValue>(string propertyName, bool mustExist = true)
Parameters
Type | Name | Description |
---|---|---|
System.String | propertyName | The name of the property. |
System.Boolean | mustExist | A value indicating whether the property and its setter must exist. |
Returns
Type | Description |
---|---|
System.Action<TDeclaring, TValue> | A property setter function. If |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | propertyName |
System.ArgumentException | Value can't be empty or consist only of white-space characters. - |
System.InvalidOperationException | Could not find property setter for |
EmitPropertySetterUnsafe<TDeclaring, TValue>(PropertyInfo)
Emits a property setter.
Declaration
public static Action<TDeclaring, TValue> EmitPropertySetterUnsafe<TDeclaring, TValue>(PropertyInfo propertyInfo)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.PropertyInfo | propertyInfo | The property info. |
Returns
Type | Description |
---|---|
System.Action<TDeclaring, TValue> | A property setter function. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Occurs when |
System.ArgumentException | Occurs when the property has no setter. |
System.ArgumentException | Occurs when |