ToolBX.WrapperMeister 2.2.0

dotnet add package ToolBX.WrapperMeister --version 2.2.0
NuGet\Install-Package ToolBX.WrapperMeister -Version 2.2.0
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="ToolBX.WrapperMeister" Version="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ToolBX.WrapperMeister --version 2.2.0
#r "nuget: ToolBX.WrapperMeister, 2.2.0"
#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 ToolBX.WrapperMeister as a Cake Addin
#addin nuget:?package=ToolBX.WrapperMeister&version=2.2.0

// Install ToolBX.WrapperMeister as a Cake Tool
#tool nuget:?package=ToolBX.WrapperMeister&version=2.2.0

WrapperMeister

Base wrapper class and interface to seamlessly wrap any .NET type.

What is it?

The Wrapper<T> class comes with a few overloads such as Equals, GetHashCode, ToString and others that are automatically mapped to the wrapped object. This allows you to use the wrapper as if it was the wrapped object.

You can access the wrapped object using the Unwrapped property or the explicit casting operator.

var actualObject = wrapper.Unwrapped;

var actualObject = (ActualType)wrapper;

Why use it?

The Wrapper<T> class is useful when you want to add functionality to an existing class without modifying it. It is also useful when you want to wrap a class that you don't own.

The most common use case is when you need to mock a class that is not virtual, abstract or lacks an interface. You can create a wrapper that inherits from the class you want to mock and map its methods and properties.

If you need a working real-world example, Wrapper<T> is used extensively in the ToolBX.NetAbstractions library.

Getting started

First, create a class that inherits from Wrapper<T> and implements IWrapper<T>.

public class MyWrapper : Wrapper<MyType>
{
	public int MyProperty
	{
		get => Unwrapped.MyProperty;
		set => Unwrapped.MyProperty = value;
	}

	//TODO : Map other properties and methods to the wrapped object

	public MyWrapper(MyType wrappedObject) : base(wrappedObject)
	{
	}
}

Then, you can use the wrapper as if it was the wrapped object.

var wrapper = new MyWrapper(new MyType());

wrapper.MyProperty = 42;

IDisposable

If the wrapped object implements IDisposable, the wrapper should also implement IDisposable and dispose the wrapped object when it is called but this mecahnism isn't built-in with Wrapper<T> so you have to do it yourself.

public class MyWrapper : Wrapper<MyType>,  IDisposable
{
	public MyWrapper(MyType wrappedObject) : base(wrappedObject)
	{
	}

	public void Dispose()
	{
		Unwrapped.Dispose();
	}
}
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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.
  • net7.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ToolBX.WrapperMeister:

Package Downloads
ToolBX.NetAbstractions

Abstractions for .NET base types such as File and Directory to provide easier means to mock low-level operations.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.0 660 1/13/2024
2.2.0-beta1 162 12/21/2023