MentalDesk.Fuse
1.2.0
dotnet add package MentalDesk.Fuse --version 1.2.0
NuGet\Install-Package MentalDesk.Fuse -Version 1.2.0
<PackageReference Include="MentalDesk.Fuse" Version="1.2.0" />
paket add MentalDesk.Fuse --version 1.2.0
#r "nuget: MentalDesk.Fuse, 1.2.0"
// Install MentalDesk.Fuse as a Cake Addin #addin nuget:?package=MentalDesk.Fuse&version=1.2.0 // Install MentalDesk.Fuse as a Cake Tool #tool nuget:?package=MentalDesk.Fuse&version=1.2.0
fuse
Fuse is a tiny package that let's you attach properties to .NET types dynamically at runtime. This can be useful if you want to associate data or meta-data with types that you can't change.
The SetFused
and GetFused
methods can be used to set/get arbitrary properties on any .net object. In the following
example an instance of MyClass
is assigned to the fused property foobar
on myObject
:
var myObject = "A simple string";
var myClass = new MyClass { Foo = 42, Bar = "Success!" };
myObject.SetFused("foobar", myClass);
var luckyNumber = myObject.GetFused<MyClass>("foobar")!.Foo;
var result = myObject.GetFused<MyClass>("foobar")!.Bar;
class `MyClass`
{
public int Foo {get; set; }
public string Bar {get; set; }
}
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. In the following example an instance of MyClass
is
assigned to the fused property MyClass
on myObject
:
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; }
}
If you don't need to fuse more than one instance of a particular type with an object, you might find this syntax more concise.
Fused
Finally, the Fused
extension lets you automatically create and bolt an additional property onto any object. For this
to work, the Type you're fusing must have a parameterless constructor.
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 | 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 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- No dependencies.
-
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.