Click or drag to resize

InterceptionRuleAttribute Class

Adds weaving rules to the interceptor.
These rules only decides whether an interceptor is weaved or not.
An interceptor can have multiple rules.
Inheritance Hierarchy
SystemObject
  SystemAttribute
    Cauldron.InterceptionInterceptionRuleAttribute

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

The InterceptionRuleAttribute type exposes the following members.

Constructors
  NameDescription
Public methodInterceptionRuleAttribute(InterceptionRuleOptions, Type)
Initializes a new instance of InterceptionRuleAttribute.
Public methodInterceptionRuleAttribute(InterceptionRuleOptions, Type, Mode)
Initializes a new instance of InterceptionRuleAttribute.
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 example shows the usage of the DoNotInterceptIfDecorated rule option.
[InterceptionRule(InterceptionRuleOptions.DoNotInterceptIfDecorated, typeof(BroadcastChangeDoNotInterceptAttribute))]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class BroadcastChangeAttribute : Attribute, IPropertyInterceptor
{
    ...
}
The attribute used to suppress the weaving of an interceptor can be any custom attribute.
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class BroadcastChangeDoNotInterceptAttribute : Attribute
{
}
If the 'BroadcastChange' interceptor is applied to a class and you wish that one of its properties is not intercepted you can decorate that property with our defined 'BroadcastChangeDoNotIntercept' attribute.
[BroadcastChange]
public class Human
{
    public double Height { get; set; }
    public double Weight { get; set; }
    public string Name { get; set; }
    [BroadcastChangeDoNotIntercept]
    public Dna DnaInformation { get; set; }
}
The weaver will not weave the 'BroadcastChange' interceptor to the 'DnaInformation' property.
The following will also end up not being weaved.
public class Human
{
    public double Height { get; set; }
    public double Weight { get; set; }
    public string Name { get; set; }

    [BroadcastChange]
    [BroadcastChangeDoNotIntercept]
    public Dna DnaInformation { get; set; }
}
The following is also possible:
[InterceptionRule(InterceptionRuleOptions.DoNotInterceptIfDecorated, typeof(BroadcastChangeDoNotInterceptAttribute))]
[InterceptionRule(InterceptionRuleOptions.DoNotInterceptIfDecorated, typeof(GuidAttribute))]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class BroadcastChangeAttribute : Attribute, IPropertyInterceptor
{
    ...
}
The weaver will not weave the 'BroadcastChange' interceptor to the 'DnaInformation' and the 'Name' property.
[BroadcastChange]
public class Human
{
    public double Height { get; set; }
    public double Weight { get; set; }
    [Guid("BB8809B1-1B45-4F47-9C53-F8763A562C50")]
    public string Name { get; set; }
    [BroadcastChangeDoNotIntercept]
    public Dna DnaInformation { get; set; }
}
The following example shows the usage of the IsImplementingInterfaceOrInheritBaseClass rule option.
[InterceptionRule(InterceptionRuleOptions.IsImplementingInterfaceOrInheritBaseClass, typeof(INotifyPropertyChanged))]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class PropertyChangedAttribute : Attribute, IPropertyInterceptor
{
    ...
}
This sample interceptor will be weaved if the class it is decorating is implementing the INotifyPropertyChanged interface.
[PropertyChanged]
public class BreadViewModel : INotifyPropertyChanged
{
    public double Height { get; set; }
    public double Weight { get; set; }
    public string Name { get; set; }
}
The weaver will also weave the following...
public class BreadViewModel : INotifyPropertyChanged
{
    public double Height { get; set; }
    public double Weight { get; set; }
    [PropertyChanged]
    public string Name { get; set; }
}
... While the following is not weaved, because of the lacking interface.
public class BreadViewModel
{
    public double Height { get; set; }
    public double Weight { get; set; }
    [PropertyChanged]
    public string Name { get; set; }
}
The following example shows a combination of rules.
[InterceptionRule(InterceptionRuleOptions.IsImplementingInterfaceOrInheritBaseClass, typeof(INotifyDataErrorInfo))]
[InterceptionRule(InterceptionRuleOptions.IsImplementingInterfaceOrInheritBaseClass, typeof(IDisposable))]
[InterceptionRule(InterceptionRuleOptions.IsImplementingInterfaceOrInheritBaseClass, typeof(INotifyPropertyChanged), Mode.Required)]
[InterceptionRule(InterceptionRuleOptions.DoNotInterceptIfDecorated, typeof(DoNotInterceptAttribute))]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class PropertyChangedAttribute : Attribute, IPropertyInterceptor
{
    ...
}
The PropertyChanged interceptor in the following implementation example will be weaved in all properties except for the 'Name' property.
[PropertyChanged]
public class BreadViewModel : INotifyPropertyChanged
{
    public double Height { get; set; }
    public double Weight { get; set; }
    [DoNotIntercept]
    public string Name { get; set; }
}
While the rules are specifying 3 different interfaces to be implemented, only the 'INotifyPropertyChanged' has the mode 'Required'. This means that the class has to implement 'INotifyPropertyChanged', which renders the other specified interface rules useless.
The following will also end up not being weaved.
[PropertyChanged]
public class BreadViewModel : INotifyDataErrorInfo
{
    public double Height { get; set; }
    public double Weight { get; set; }
    [DoNotIntercept]
    public string Name { get; set; }
}
If we remove the mode of the third interface rule...
[InterceptionRule(InterceptionRuleOptions.IsImplementingInterfaceOrInheritBaseClass, typeof(INotifyDataErrorInfo))]
[InterceptionRule(InterceptionRuleOptions.IsImplementingInterfaceOrInheritBaseClass, typeof(IDisposable))]
[InterceptionRule(InterceptionRuleOptions.IsImplementingInterfaceOrInheritBaseClass, typeof(INotifyPropertyChanged))]
[InterceptionRule(InterceptionRuleOptions.DoNotInterceptIfDecorated, typeof(DoNotInterceptAttribute))]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class PropertyChangedAttribute : Attribute, IPropertyInterceptor
{
    ...
}
... And decorate the 'BreadViewModel' class with it; The weaver will now weave the 'PropertyChanged' interceptor. The rules are now specifying that the decorated class has to implement at least one of the 3 defined interfaces.
[PropertyChanged]
public class BreadViewModel : INotifyDataErrorInfo
{
    public double Height { get; set; }
    public double Weight { get; set; }
    [DoNotIntercept]
    public string Name { get; set; }
}
Adding the mode 'Required' to all interface rules will specify that the decorated class has to implement all 3 interfaces or else the interceptor is not weaved.
[InterceptionRule(InterceptionRuleOptions.IsImplementingInterfaceOrInheritBaseClass, typeof(INotifyDataErrorInfo), Mode.Required)]
[InterceptionRule(InterceptionRuleOptions.IsImplementingInterfaceOrInheritBaseClass, typeof(IDisposable), Mode.Required)]
[InterceptionRule(InterceptionRuleOptions.IsImplementingInterfaceOrInheritBaseClass, typeof(INotifyPropertyChanged), Mode.Required)]
[InterceptionRule(InterceptionRuleOptions.DoNotInterceptIfDecorated, typeof(DoNotInterceptAttribute))]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class PropertyChangedAttribute : Attribute, IPropertyInterceptor
{
    ...
}
[PropertyChanged]
public class BreadViewModel : INotifyDataErrorInfo, IDisposable, INotifyPropertyChanged
{
    public double Height { get; set; }
    public double Weight { get; set; }
    [DoNotIntercept]
    public string Name { get; set; }

    ...
}
See Also