BlazorParameterCastingMagic 1.2.23060811

dotnet add package BlazorParameterCastingMagic --version 1.2.23060811                
NuGet\Install-Package BlazorParameterCastingMagic -Version 1.2.23060811                
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="BlazorParameterCastingMagic" Version="1.2.23060811" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BlazorParameterCastingMagic --version 1.2.23060811                
#r "nuget: BlazorParameterCastingMagic, 1.2.23060811"                
#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 BlazorParameterCastingMagic as a Cake Addin
#addin nuget:?package=BlazorParameterCastingMagic&version=1.2.23060811

// Install BlazorParameterCastingMagic as a Cake Tool
#tool nuget:?package=BlazorParameterCastingMagic&version=1.2.23060811                

BlazorParameterCastingMagic

Overview

The BlazorParameterCastingMagic package provides a way to handle dynamic casting of parameters in Blazor components. It is a convenient and flexible way to use implicit casting within your Blazor components to handle parameter values.

Blazor does not natively support implicit conversions for parameters. This means that even if you've defined implicit conversions in your classes, Blazor will not use them when trying to convert values for parameters in your components. This leads to casting exceptions when you try to use different types of values for the parameters.

This package introduces a workaround for this problem. It allows developers to apply dynamic casting to the parameters and bypass the casting exceptions that occur when different types of values are used for parameters. If implicit casting is not possible, the package will attempt to map the types using Nextended.Core.

Installation

Provide instructions for installing your package here.

Usage

Firstly, you need to define the AllowMagicCastingAttribute on the parameters of your components where you want to enable dynamic casting.

[Parameter]
[AllowMagicCasting]
public FlexibleParameter Size { get; set; } = 1;

Then, you need to call the ApplyMagicCasting extension method on the parameters in the SetParametersAsync method of your component.

public override Task SetParametersAsync(ParameterView parameters)
{           
    return base.SetParametersAsync(parameters.ApplyMagicCasting(this));
}

You can now use different types of values for your parameters without running into casting exceptions.

Example 1

You can use a string value for the parameter in your component.

<MyComponent Size="@("11px")" />

Example 2

You can also use a number value for the parameter in your component.

<MyComponent Size="8" />

Both of these examples will work without throwing a casting exception.

Example with a Custom Class

Here is an example of a custom class FlexibleParameter that you can use with the AllowMagicCastingAttribute:

public class FlexibleParameter
{
    public int IntValue { get; set; }
    public string StringValue { get; set; }

    public static implicit operator FlexibleParameter(int intValue)
    {
        return new FlexibleParameter { IntValue = intValue };
    }

    public static implicit operator FlexibleParameter(string stringValue)
    {
        return new FlexibleParameter { StringValue = stringValue };
    }
}

This class has two properties IntValue and StringValue, and implicit conversion operators for int and string. You can use this class for a parameter in your component and apply the AllowMagicCastingAttribute.

Limitations

The BlazorParameterCastingMagic package uses reflection to handle dynamic casting which can be less performant than direct casting. This should not be an issue unless there is a very large number of parameters.

Conclusion

The BlazorParameterCastingMagic package provides a workaround for the lack of support for implicit conversions in Blazor. With this package, you can avoid casting exceptions and use different types of values for your component parameters.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on BlazorParameterCastingMagic:

Package Downloads
MudBlazor.Extensions

MudBlazor.Extensions is a small extension library for MudBlazor from https://mudblazor.com/

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on BlazorParameterCastingMagic:

Repository Stars
fgilde/MudBlazor.Extensions
MudBlazor.Extensions from https://www.mudex.org is a small extension for MudBlazor from https://mudblazor.com
Version Downloads Last updated
1.2.23060811 70,975 6/8/2023
1.2.23060721 165 6/7/2023
1.2.23060720 161 6/7/2023