ToolBX.WrapperMeister
2.2.0-beta1
See the version list below for details.
dotnet add package ToolBX.WrapperMeister --version 2.2.0-beta1
NuGet\Install-Package ToolBX.WrapperMeister -Version 2.2.0-beta1
<PackageReference Include="ToolBX.WrapperMeister" Version="2.2.0-beta1" />
paket add ToolBX.WrapperMeister --version 2.2.0-beta1
#r "nuget: ToolBX.WrapperMeister, 2.2.0-beta1"
// Install ToolBX.WrapperMeister as a Cake Addin #addin nuget:?package=ToolBX.WrapperMeister&version=2.2.0-beta1&prerelease // Install ToolBX.WrapperMeister as a Cake Tool #tool nuget:?package=ToolBX.WrapperMeister&version=2.2.0-beta1&prerelease
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 override the properties and methods you want to mock.
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>`.
```cs
public class MyWrapper : Wrapper<MyType>, IWrapper<MyType>
{
//TODO : Map 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 | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
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 |
---|---|---|
3.0.0 | 1,198 | 9/23/2024 |
3.0.0-beta1 | 158 | 8/29/2024 |
2.2.0 | 1,168 | 1/13/2024 |
2.2.0-beta1 | 289 | 12/21/2023 |