Search Results for

    Show / Hide Table of Contents
    View Source

    Class ReflectionUtilities

    Provides utilities to simplify reflection.

    Inheritance
    System.Object
    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 Source

    EmitConstructor<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 mustExist is false, returns null when the constructor does not exist.

    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).

    View Source

    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.

    View Source

    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.

    View Source

    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.

    View Source

    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.

    View Source

    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.

    View Source

    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.

    View Source

    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 mustExist is false, returns null when the method does not exist.

    Type Parameters
    Name Description
    TLambda

    A lambda representing the method.

    Remarks

    The method arguments are determined by TLambda generic arguments.

    View Source

    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 mustExist is false, returns null when the method does not exist.

    Type Parameters
    Name Description
    TLambda

    A lambda representing the method.

    Remarks

    The method arguments are determined by TLambda generic arguments.

    View Source

    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 mustExist is false, returns null when the method does not exist.

    Type Parameters
    Name Description
    TDeclaring

    The declaring type.

    TLambda

    A lambda representing the method.

    Remarks

    The method arguments are determined by TLambda generic arguments.

    View Source

    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.

    View Source

    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.

    View Source

    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 mustExist is false, returns null when the property or its getter does not exist.

    Type Parameters
    Name Description
    TDeclaring

    The declaring type.

    TValue

    The property type.

    View Source

    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.

    View Source

    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 mustExist is false, returns null when the property or its getter or setter does not exist.

    Type Parameters
    Name Description
    TDeclaring

    The declaring type.

    TValue

    The property type.

    View Source

    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.

    View Source

    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 mustExist is false, returns null when the property or its setter does not exist.

    Type Parameters
    Name Description
    TDeclaring

    The declaring type.

    TValue

    The property type.

    View Source

    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.

    • Improve this Doc
    • View Source
    In This Article
    • Methods
      • EmitConstructor<TLambda>(Boolean, Nullable<Type>)
      • EmitConstructor<TLambda>(ConstructorInfo)
      • EmitConstructorUnsafe<TLambda>(ConstructorInfo)
      • EmitFieldGetter<TDeclaring, TValue>(String)
      • EmitFieldGetterAndSetter<TDeclaring, TValue>(String)
      • EmitFieldSetter<TDeclaring, TValue>(String)
      • EmitMethod<TLambda>(MethodInfo)
      • EmitMethod<TLambda>(String, Boolean)
      • EmitMethod<TLambda>(Type, String, Boolean)
      • EmitMethod<TDeclaring, TLambda>(String, Boolean)
      • EmitMethodUnsafe<TLambda>(MethodInfo)
      • EmitPropertyGetter<TDeclaring, TValue>(PropertyInfo)
      • EmitPropertyGetter<TDeclaring, TValue>(String, Boolean)
      • EmitPropertyGetterAndSetter<TDeclaring, TValue>(PropertyInfo)
      • EmitPropertyGetterAndSetter<TDeclaring, TValue>(String, Boolean)
      • EmitPropertySetter<TDeclaring, TValue>(PropertyInfo)
      • EmitPropertySetter<TDeclaring, TValue>(String, Boolean)
      • EmitPropertySetterUnsafe<TDeclaring, TValue>(PropertyInfo)
    Back to top Copyright © 2016-present Umbraco
    Generated by DocFX