MentalDesk.Fuse
1.1.0
See the version list below for details.
dotnet add package MentalDesk.Fuse --version 1.1.0
NuGet\Install-Package MentalDesk.Fuse -Version 1.1.0
<PackageReference Include="MentalDesk.Fuse" Version="1.1.0" />
paket add MentalDesk.Fuse --version 1.1.0
#r "nuget: MentalDesk.Fuse, 1.1.0"
// Install MentalDesk.Fuse as a Cake Addin #addin nuget:?package=MentalDesk.Fuse&version=1.1.0 // Install MentalDesk.Fuse as a Cake Tool #tool nuget:?package=MentalDesk.Fuse&version=1.1.0
fuse
Fuse is a tiny package that let's you attach properties to .NET types dynamically at runtime.
It provides 3 extension methods that can be used with any object?
:
- SetFused
- GetFused
- Fused
SetFused
The SetFused
method can be used to associate or attach an arbitrary property to any .net object using a string key.
var myObject = "A simple string";
var myClass = new MyClass { Foo = 42, Bar = "Success!" };
myObject.SetFused("foobar", myClass);
class MyClass
{
public int Foo {get; set; }
public string Bar {get; set; }
}
GetFused
The GetFused
method can be used to retrieve properties that have been fused to an object.
var luckyNumber = myObject.GetFused<MyClass>("foobar")!.Foo;
var result = myObject.GetFused<MyClass>("foobar")!.Bar;
Generic overloads
Alternatively you can use the Generic overloads, SetFused<T>
and GetFused<T>
, which automatically assign a property
name based on the type of the value being stored/retrieved.
var myObject = "A simple string";
var myClass = new MyClass { Foo = 42, Bar = "Success!" };
myObject.SetFused<MyClass>(myClass);
var luckyNumber = myObject.GetFused<MyClass>()!.Foo;
var result = myObject.GetFused<MyClass>()!.Bar;
class MyClass
{
public int Foo {get; set; }
public string Bar {get; set; }
}
Fused
Finally, the Fused
extension lets you automatically create and bolt an additional property onto any object.
You can see an example of how this is used in the Sample
application, but it looks something like this in action:
// We start with a humble string
string brendan = "Brendan Eich (/ˈaɪk/; born July 4, 1961)";
UnpackDetails(brendan);
// And now our string has a aggregate Person property fused to it... no initialisation necessary - Person just
// has to have a parameterless constructor
var firstName = brendan.Fused<Person>().FirstName;
void UnpackDetails(string input)
{
Regex regex = new Regex(@"(?<first>\w+) (?<last>\w+) \(.*; born (?<dob>.+)\)");
Match match = regex.Match(input);
var dob = match.Groups["dob"].Value;
// Here we fuse some additional properties to the string so that we can use these later on
input.Fused<Person>().FirstName = match.Groups["first"].Value;
input.Fused<Person>().LastName = match.Groups["last"].Value;
input.Fused<Person>().DateOfBirth = DateTime.ParseExact(dob, "MMMM d, yyyy", CultureInfo.InvariantCulture);
}
class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public DateTime DateOfBirth { get; set; }
}
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. |
-
net7.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.