Search Results for

    Show / Hide Table of Contents
    View Source

    Class ReflectionUtilities

    Provides utilities to simplify reflection.

    Inheritance
    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

    EmitConstructorUnsafe<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 TLambda is not a Func or when its generic arguments do not match those of ctor.

    ArgumentNullException

    Occurs when ctor is null.

    View Source

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

    Exceptions
    Type Condition
    InvalidOperationException

    Occurs when the constructor does not exist and mustExist is true.

    ArgumentException

    Occurs when TLambda is not a Func or when declaring is specified and does not match the function's returned type.

    View Source

    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 TLambda is not a Func or when its generic arguments do not match those of ctor.

    ArgumentNullException

    Occurs when ctor is null.

    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
    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. - fieldName or Value type TValue does not match field TDeclaring. fieldName type.

    InvalidOperationException

    Could not find field TDeclaring. fieldName.

    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
    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. - fieldName or Value type TValue does not match field TDeclaring. fieldName type.

    InvalidOperationException

    Could not find field TDeclaring. fieldName.

    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
    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. - fieldName or Value type TValue does not match field TDeclaring. fieldName type.

    InvalidOperationException

    Could not find field TDeclaring. fieldName.

    View Source

    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 method is null.

    ArgumentException

    Occurs when Occurs when TLambda does not match the method signature.

    View Source

    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 method is null.

    ArgumentException

    Occurs when Occurs when TLambda does not match the method signature.

    View Source

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

    Exceptions
    Type Condition
    ArgumentNullException

    methodName

    ArgumentException

    Value can't be empty or consist only of white-space characters. - methodName or Occurs when TLambda does not match the method signature..

    InvalidOperationException

    Occurs when no proper method with name methodName could be found.

    View Source

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

    Exceptions
    Type Condition
    ArgumentNullException

    methodName

    ArgumentException

    Value can't be empty or consist only of white-space characters. - methodName or Occurs when TLambda does not match the method signature..

    InvalidOperationException

    Occurs when no proper method with name methodName could be found.

    View Source

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

    Exceptions
    Type Condition
    ArgumentNullException

    methodName

    ArgumentException

    Value can't be empty or consist only of white-space characters. - methodName or Occurs when TLambda does not match the method signature..

    InvalidOperationException

    Occurs when no proper method with name methodName could be found.

    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
    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 propertyInfo is null.

    ArgumentException

    Occurs when the property has no getter or no setter.

    ArgumentException

    Occurs when TValue does not match the type of the property.

    View Source

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

    Exceptions
    Type Condition
    ArgumentNullException

    propertyName

    ArgumentException

    Value can't be empty or consist only of white-space characters. - propertyName or Value type TValue does not match property TDeclaring. propertyName type.

    InvalidOperationException

    Could not find property getter and setter for TDeclaring.propertyName.

    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
    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 propertyInfo is null.

    ArgumentException

    Occurs when the property has no getter.

    ArgumentException

    Occurs when TValue does not match the type of the property.

    View Source

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

    Exceptions
    Type Condition
    ArgumentNullException

    propertyName

    ArgumentException

    Value can't be empty or consist only of white-space characters. - propertyName or Value type TValue does not match property TDeclaring. propertyName type.

    InvalidOperationException

    Could not find property getter for TDeclaring. propertyName.

    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
    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 propertyInfo is null.

    ArgumentException

    Occurs when the property has no setter.

    ArgumentException

    Occurs when TValue does not match the type of the property.

    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
    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 propertyInfo is null.

    ArgumentException

    Occurs when the property has no setter.

    ArgumentException

    Occurs when TValue does not match the type of the property.

    View Source

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

    Exceptions
    Type Condition
    ArgumentNullException

    propertyName

    ArgumentException

    Value can't be empty or consist only of white-space characters. - propertyName or Value type TValue does not match property TDeclaring. propertyName type.

    InvalidOperationException

    Could not find property setter for TDeclaring. propertyName.

    • Edit this page
    • View Source
    In this article
    Back to top Copyright © 2016-present Umbraco
    Generated by DocFX