![]() |
VOOZH | about |
dotnet add package BlazorParameterCastingMagic --version 1.2.25121215
NuGet\Install-Package BlazorParameterCastingMagic -Version 1.2.25121215
<PackageReference Include="BlazorParameterCastingMagic" Version="1.2.25121215" />
<PackageVersion Include="BlazorParameterCastingMagic" Version="1.2.25121215" />Directory.Packages.props
<PackageReference Include="BlazorParameterCastingMagic" />Project file
paket add BlazorParameterCastingMagic --version 1.2.25121215
#r "nuget: BlazorParameterCastingMagic, 1.2.25121215"
#:package BlazorParameterCastingMagic@1.2.25121215
#addin nuget:?package=BlazorParameterCastingMagic&version=1.2.25121215Install as a Cake Addin
#tool nuget:?package=BlazorParameterCastingMagic&version=1.2.25121215Install as a Cake Tool
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.
Provide instructions for installing your package here.
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.
You can use a string value for the parameter in your component.
<MyComponent Size="@("11px")" />
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.
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.
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.
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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
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/ |
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.25121215 | 50,545 | 12/12/2025 |
| 1.2.25112218 | 12,643 | 11/22/2025 |
| 1.2.25111314 | 708 | 11/13/2025 |
| 1.2.25110914 | 26,636 | 11/9/2025 |
| 1.2.25102110 | 7,258 | 10/21/2025 |
| 1.2.23060811 | 284,971 | 6/8/2023 |
| 1.2.23060721 | 349 | 6/7/2023 |
| 1.2.23060720 | 314 | 6/7/2023 |