Pygmalions.Prism.Decorating 1.0.2

dotnet add package Pygmalions.Prism.Decorating --version 1.0.2                
NuGet\Install-Package Pygmalions.Prism.Decorating -Version 1.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Pygmalions.Prism.Decorating" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pygmalions.Prism.Decorating --version 1.0.2                
#r "nuget: Pygmalions.Prism.Decorating, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Pygmalions.Prism.Decorating as a Cake Addin
#addin nuget:?package=Pygmalions.Prism.Decorating&version=1.0.2

// Install Pygmalions.Prism.Decorating as a Cake Tool
#tool nuget:?package=Pygmalions.Prism.Decorating&version=1.0.2                

Prism Decorating

This plugin provides a mechanism to modify the behavior of functions in run-time.

How to Use

By simply mark [Decorate] attribute on any non-final virtual method of a class, this plugin will implement the IDecorated interface in the proxy class. Method decorators can be get through GetMethodDecorator(MethodInfo) method provided by IDecorated interface.

For example, this is the code of a sample class:

public class SampleObject
{
    [Decorate]
    public virtual int Value { get => BackingValue; set => BackingValue = value; }

    public int BackingValue = 0;

    [Decorate]
    public virtual int Add(int value) => BackingValue + value;
    
    public SampleObject()
    {}

    public SampleObject(int initialValue)
    {
        BackingValue = initialValue;
    }
}

Firstly, initialize a proxy generator and load this plugin:

// Initialize a proxy generator.
var generator = new Generator();
// Load decoration plugin.
generator.RegisterPlugin(new DecorationPlugin());

Secondly, create a instance of the proxy class:

var instance = Activator.CreateInstance(generator.GetProxy<SampleObject>());

Convert this instance into the original type and the IDecorated interface separately:

var proxy = (IDecorated)instance!;
var sample = (SampleObject)instance!;

Get the decorator of Add(int) method. In this example, the code in that method will be skipped, and the return value will be set to 10.

var decorator = 
    proxy.GetMethodDecorator(typeof(SampleObject).GetMethod(nameof(SampleObject.Add))!);

decorator!.Invoking += (ref Invocation invocation) =>
{
    invocation.Result = 10;
    invocation.Skipped = true;
};

Invoke the decorated method, the result here will be 10, no matter what the argument is given.

var result = sample.Add(3);
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 412 10/31/2022
1.0.1 370 10/17/2022
1.0.0 370 8/3/2022