Typesafe.With 1.0.1-alpha0002

This is a prerelease version of Typesafe.With.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Typesafe.With --version 1.0.1-alpha0002                
NuGet\Install-Package Typesafe.With -Version 1.0.1-alpha0002                
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="Typesafe.With" Version="1.0.1-alpha0002" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Typesafe.With --version 1.0.1-alpha0002                
#r "nuget: Typesafe.With, 1.0.1-alpha0002"                
#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 Typesafe.With as a Cake Addin
#addin nuget:?package=Typesafe.With&version=1.0.1-alpha0002&prerelease

// Install Typesafe.With as a Cake Tool
#tool nuget:?package=Typesafe.With&version=1.0.1-alpha0002&prerelease                

Typesafe.With

Providing with-functionality to any type on the .NET platform.

What and Why?

Typesafe.With is a strongly typed way to change properties on immutable types that lets you:

  • Avoid writing with methods by hand ever again
  • Update properties on any type

Features

  • Robust and well tested
  • Self contained with no dependencies
  • Easily installed through NuGet
  • Supports .NET Core (.NET Standard 2+)
  • Focused but full-featured API

Supports

Typesafe.With supports types using:

  • Constructors
  • Property setters
  • A mix of constructors and property setters

Installation

PM> Install-Package Typesafe.With

Concepts

Typesafe.With allows you to update properties on immutable types in a type safe way that is type checked by the compiler.

Typesafe.With provides a single method called With. Any call to With produces a new instance of the immutable type with the updated property.

The call to With is strongly typed which means that the compiler verifies that the assignment is valid.

Usage

Using the library

To use Typesafe.With simply import the following namespace:

using Typesafe.With;

Update a property

public class Person
{
    public string Name { get; }
    
    public Person(string name) => (Name) = (name);
}

var harry = new Person("Harry Potter");
var hermione = harry.With(p => p.Name, "Hermione Granger");

Console.WriteLine(hermione.Name); // Prints "Hermione Granger"

Updating multiple properties

public enum House { Gryffindor, Slytherin }

public class Person
{
    public string Name { get; }
    public House House { get; }
    
    public Person(string name, House house) => (Name, House) = (name, house);
}

var harry = new Person("Harry Potter", House.Gryffindor);
var malfoy = harry
	.With(p => p.Name, "Malfoy")
	.With(p => p.House, House.Slytherin);

Console.WriteLine(malfoy.Name); // Prints "Malfoy"
Console.WriteLine(malfoy.House); // Prints "Slytherin"

Convention

Typesafe.With relies on the convention that the property is named the same in the type constructor.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

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.2.0 322 3/6/2024
1.1.1 4,547 7/12/2023
1.1.0 3,758 10/11/2022
1.0.2 10,476 1/9/2021
1.0.1 634 11/27/2020
1.0.1-alpha0002 342 11/23/2020
1.0.1-alpha0001 386 11/23/2020
1.0.0-alpha 333 11/20/2020

* Validate whether property can be set before constructing the instance
* Skip non-writable properties when copying properties