Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mentaldesk/fuse

A tiny little package that let's you add properties to .NET types dynamically at runtime.
https://github.com/mentaldesk/fuse

dotnet dynamic-programming nuget-package utilities

Last synced: 3 months ago
JSON representation

A tiny little package that let's you add properties to .NET types dynamically at runtime.

Awesome Lists containing this project

README

        

# 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`:

```csharp
var myObject = "A simple string";
var myClass = new MyClass { Foo = 42, Bar = "Success!" };
myObject.SetFused("foobar", myClass);

var luckyNumber = myObject.GetFused("foobar")!.Foo;
var result = myObject.GetFused("foobar")!.Bar;

class `MyClass`
{
public int Foo {get; set; }
public string Bar {get; set; }
}
```

## Generic overloads

Alternatively you can use the Generic overloads, `SetFused` and `GetFused`, 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`:

```csharp
var myObject = "A simple string";
var myClass = new MyClass { Foo = 42, Bar = "Success!" };
myObject.SetFused(myClass);

var luckyNumber = myObject.GetFused()!.Foo;
var result = myObject.GetFused()!.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:

```csharp
// 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().FirstName;

void UnpackDetails(string input)
{
Regex regex = new Regex(@"(?\w+) (?\w+) \(.*; born (?.+)\)");
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().FirstName = match.Groups["first"].Value;
input.Fused().LastName = match.Groups["last"].Value;
input.Fused().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; }
}
```