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 SourceEmitConstructor<TLambda>(Boolean, Nullable<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.Nullable<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
).
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. |
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.
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 |
---|---|
Func<TDeclaring, TValue> | A field getter function. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The field type. |
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<Func<TDeclaring, TValue>, Action<TDeclaring, TValue>> | A field getter and setter functions. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The field type. |
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 |
---|---|
Action<TDeclaring, TValue> | A field setter action. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The field type. |
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. |
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.
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 |
---|---|---|
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.
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.
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. |
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 |
---|---|
Func<TDeclaring, TValue> | A property getter function. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |
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.Nullable<Func<TDeclaring, TValue>> | A property getter function. If |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |
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<Func<TDeclaring, TValue>, Action<TDeclaring, TValue>> | A property getter and setter functions. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |
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<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. |
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 |
---|---|
Action<TDeclaring, TValue> | A property setter function. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |
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.Nullable<Action<TDeclaring, TValue>> | A property setter function. If |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |
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 |
---|---|
Action<TDeclaring, TValue> | A property setter function. |
Type Parameters
Name | Description |
---|---|
TDeclaring | The declaring type. |
TValue | The property type. |