Click or drag to resize

AssignMethodAttribute Class

Indicates the weaver to find the matching method and assign it to the Action or FuncTResult
This attribute can only be applied to public non-static fields of type Action and FuncTResult; with or without arguments.
If the field's type is an Action, the weaver will search for a void method without arguments.
If the field's type is an ActionT, the weaver will search for a void method with 1 argument.
If the field's type in a FuncTResult, the weaver will search for a method with a return type that matches TResult and no argmuents.
If the field's type in a FuncT, TResult, the weaver will search for a method with a return type that matches TResult and 1 argmuent.

This interceptor extension is available for IPropertyGetterInterceptor, IPropertySetterInterceptor, IPropertyInterceptor, IMethodInterceptor, ISimpleMethodInterceptor and IConstructorInterceptor.

Since properties are only methods, the AssignMethodAttribute can also search for the property's getter and setter. The getter requires a 'get_' prefix and the setter a 'set_' prefix e.g. if the property is named 'DispatchDate' then the setter search pattern will be 'set_DispatchDate' and the setter's search pattern will be 'get_DispatchDate'.

Inheritance Hierarchy
SystemObject
  SystemAttribute
    Cauldron.InterceptionAssignMethodAttribute

Namespace:  Cauldron.Interception
Assembly:  Cauldron.Interception (in Cauldron.Interception.dll) Version: 1.0.0
Syntax
[AttributeUsageAttribute(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public sealed class AssignMethodAttribute : Attribute

The AssignMethodAttribute type exposes the following members.

Constructors
  NameDescription
Public methodAssignMethodAttribute
Initializes a new instance of AssignMethodAttribute.
Top
Properties
  NameDescription
Public propertyMethodName
Gets the name of the method to find.
Top
Extension Methods
  NameDescription
Public Extension MethodCode exampleAs(Type)Overloaded.
Converts a type using the implicit or explicit operators. If both fails it will try to convert the value with ChangeType(Object, Type).
(Defined by ExtensionsConvertions.)
Public Extension MethodCode exampleAs(Type, Type)Overloaded.
Converts a type using the implicit or explicit operators. If both fails it will try to convert the value with ChangeType(Object, Type).
(Defined by ExtensionsConvertions.)
Public Extension MethodCode exampleAsTOverloaded.
Performs a cast between compatible reference types. If a convertion is not possible then null is returned. As a last resort it will use ChangeType(Object, Type).

Tries to use the implicit and explicit operators if exists when convertion with 'as' returns null.

(Defined by ExtensionsConvertions.)
Public Extension MethodCode exampleCreateTypeT
Creates a new Type that implements the properties of an interface defined by T and copies all value of anon to the new object.
(Defined by ExtensionsInterception.)
Public Extension MethodGetPropertyNonPublicValueT
Searches for the specified property, using the specified binding constraints and returns its value.

Default BindingFlags are Instance and NonPublic

(Defined by ExtensionsReflection.)
Public Extension MethodGetPropertyValue(String, BindingFlags)Overloaded.
Searches for the specified property, using the specified binding constraints and returns its value.
(Defined by ExtensionsReflection.)
Public Extension MethodGetPropertyValueT(String)Overloaded.
Searches for the specified property, using the specified binding constraints and returns its value.

Default BindingFlags are Instance and Public

(Defined by ExtensionsReflection.)
Public Extension MethodGetPropertyValueT(String, BindingFlags)Overloaded.
Searches for the specified property, using the specified binding constraints and returns its value.
(Defined by ExtensionsReflection.)
Public Extension MethodMapToT
Maps all properties and fields of an instance to another instance. The Clone() method is used to copy an instance if exist.

Mapping fails on jagged and multidimensional array. Classes without parameterless constructor will stay null.

(Defined by ExtensionsCloning.)
Public Extension MethodToLong
Tries to convert a Object to an Int64. Returns MinValue if target cannot be parsed.
(Defined by ExtensionsConvertions.)
Public Extension MethodToStringEx(String)Overloaded.
Converts the value of this instance to its equivalent string representation, using the specified format.

The following custom formatter are already added: ByteSizeFormatter, MetricUnitFormatter

(Defined by Extensions.)
Public Extension MethodToStringEx(String, CultureInfo)Overloaded.
Converts the value of this instance to its equivalent string representation, using the specified format.

The following custom formatter are already added: ByteSizeFormatter, MetricUnitFormatter

(Defined by Extensions.)
Public Extension MethodTryDispose
Tries to performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

This will dispose an object if it implements the IDisposable interface.

(Defined by Extensions.)
Top
Examples
The following sample implementation will execute a method if a property setter is invoked. The method to execute is described by the constructor parameter 'On{Name}Set'. '{Name}' is a placeholder and will be replaced by the property's name.
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class PropertyOnSetAttribute : Attribute, IPropertySetterInterceptor
{
    [AssignMethod("On{Name}Set")]
    public Action onPropertySet;

    public void OnException(Exception e)
    {
    }

    public void OnExit()
    {
    }

    public bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue)
    {
        this.onPropertySet?.Invoke();
        return false;
    }
}
The following is a sample usage of the interceptor above.
[PropertyOnSet]
public DateTime DispatchDate { get; set; }

private void OnDispatchDateSet()
{
   // Your code that is executed if a value is assigned to the DispatchDate property.
}

The following sample interceptor implementation accepts a custom method name.
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class PropertyOnSetAttribute : Attribute, IPropertySetterInterceptor
{
    [AssignMethod("{CtorArgument:0}")]
    public Action>string, object, object< onPropertySet;

    public PropertyOnSetAttribute(string onSetPropertyMethod)
    {
    }

    public void OnException(Exception e)
    {
    }

    public void OnExit()
    {
    }

    public bool OnSet(PropertyInterceptionInfo propertyInterceptionInfo, object oldValue, object newValue)
    {
        this.onPropertySet?.Invoke(propertyInterceptionInfo.PropertyName, oldValue, newValue);
        return false;
    }
}
The '{CtorArgument:0}' placeholder tells the weaver that the name (or part of the name) of the method to assign to the 'onPropertySet' field is described by the the first constructor parameter of the 'PropertyOnSetAttribute'.
The following is a sample usage of the interceptor above.
[PropertyOnSet(nameof(AnyMethodToExecute))]
public DateTime DispatchDate { get; set; }

private void AnyMethodToExecute(string propertyName, object oldValue, object newValue)
{
   // Your code that is executed if a value is assigned to the DispatchDate property.
}
See Also