Drastic.Transmute
0.5.7
dotnet add package Drastic.Transmute --version 0.5.7
NuGet\Install-Package Drastic.Transmute -Version 0.5.7
<PackageReference Include="Drastic.Transmute" Version="0.5.7" />
paket add Drastic.Transmute --version 0.5.7
#r "nuget: Drastic.Transmute, 0.5.7"
// Install Drastic.Transmute as a Cake Addin #addin nuget:?package=Drastic.Transmute&version=0.5.7 // Install Drastic.Transmute as a Cake Tool #tool nuget:?package=Drastic.Transmute&version=0.5.7
Drastic.Transmute
Drastic.Transmute is a port of Transmute to .NET. It supports .NET Standard 2.0 and .NET 8.0 (Standard, iOS, Catalyst, tvOS, and Android).
This exists to port other libraries that depend on it. If you depend on this library, it would be best to fork this library yourself and build on it, or switch to something else. YMMV.
Original Readme.
It's very common to convert data from a type to another and there's plenties of APIs to achieve it in .NET. Transmute was created to have a single centralized channel for all conversions.
Install
Available on NuGet
Quickstart
//.netstandard
var b = Transmuter.Default.Convert<bool>(5); // true
var d = Transmuter.Default.Convert<DateTime>(1495820216); // 26/5/2017
//Xamarin.iOS
var c = Transmuter.Default.Convert<UIColor>(0xFF0000); // red
Functionalities
Custom converter
You can register a custom converters at any time with Register
function :
Transmuter.Default.Register<T1,T2>(t1 => /* to t2 */);
If a converter already exists for those types, it will be replaced by yours.
Composability
Transmuter will try to find a way to convert a value to a the target type if a path exists.
So if you register those converters :
Transmuter.Default.Register<T1,T2>(...);
Transmuter.Default.Register<T2,T3>(...);
Transmuter.Default.Register<T3,T4>(...);
This conversions will succeed :
T1 t1 = ...;
T4 t4 = Transmuter.Default.Convert<T4>(t1); // T1 -> T2,T2 -> T3,T3 -> T4
Array conversions
If the source type and target type are arrays, and a converter has been registered to convert a source item to a target item, a converter will be available too :
var source = new[] { 10, 11, 12 };
var target = transmuter.Convert<string[]>(source); // { "10", "11", "12" }
Default converters
All platforms
From | To | Description |
---|---|---|
short | byte[] | BitConverter |
int | byte[] | BitConverter |
long | byte[] | BitConverter |
float | byte[] | BitConverter |
double | byte[] | BitConverter |
bool | byte[] | BitConverter |
byte[] | short | BitConverter |
byte[] | int | BitConverter |
byte[] | long | BitConverter |
byte[] | float | BitConverter |
byte[] | double | BitConverter |
byte[] | bool | BitConverter |
int | short | Convert.ChangeType |
short | int | Convert.ChangeType |
int | long | Convert.ChangeType |
long | int | Convert.ChangeType |
int | float | Convert.ChangeType |
float | int | Convert.ChangeType |
int | double | Convert.ChangeType |
double | int | Convert.ChangeType |
float | double | Convert.ChangeType |
double | float | Convert.ChangeType |
int | bool | > 0 ? |
bool | int | true ? 1 : 0 |
short | string | ToString |
int | string | ToString |
long | string | ToString |
float | string | ToString |
double | string | ToString |
bool | string | ToString |
long | DateTime | from timestamp (ms) |
DateTime | long | to timestamp (ms) |
Xamarin.iOS
From | To | Description |
---|---|---|
DateTime | NSDate | conversion |
NSDate | DateTime | conversion |
CGRect | int[] | { x,y,w,h } |
int[] | CGRect | { x,y,w,h } |
CGRect | float[] | { x,y,w,h } |
float[] | CGRect | { x,y,w,h } |
int | UIColor | 0xAARRGGBB |
UIColor | int | 0xAARRGGBB |
bytes[] | UIColor | AA,RR,GG,BB |
UIColor | bytes[] | AA,RR,GG,BB |
bool | UIColor | true ? UIColor.Green : UIColor.Red |
UIColor | bool | value == UIColor.Green |
string | UIImage | NSData.FromFile → UIImage.LoadFromData |
Xamarin.Android
From | To | Description |
---|---|---|
bool | ViewStates | true ? ViewStates.Visible : ViewStates.Gone |
ViewStates | bool | value == ViewStates.Visible |
int | Color | 0xAARRGGBB |
Color | int | 0xAARRGGBB |
bytes[] | Color | AA,RR,GG,BB |
Color | bytes[] | AA,RR,GG,BB |
string | Bitmap | BitmapFactory.DecodeFile |
Roadmap / Ideas
- Add collection conversion
- Manage conversion errors
- Adding Windows platform (with IValueConverter builder)
Contributions
Contributions are welcome! If you find a bug please report it and if you want a feature please report it.
If you want to contribute code please file an issue and create a branch off of the current dev branch and file a pull request.
License
MIT © Aloïs Deniel
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 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 is compatible. net8.0-android was computed. net8.0-android34.0 is compatible. net8.0-browser was computed. net8.0-ios was computed. net8.0-ios17.2 is compatible. net8.0-maccatalyst was computed. net8.0-maccatalyst17.2 is compatible. net8.0-macos was computed. net8.0-tvos was computed. net8.0-tvos17.2 is compatible. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.0
- No dependencies.
-
net8.0
- No dependencies.
-
net8.0-android34.0
- No dependencies.
-
net8.0-ios17.2
- No dependencies.
-
net8.0-maccatalyst17.2
- No dependencies.
-
net8.0-tvos17.2
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Drastic.Transmute:
Package | Downloads |
---|---|
Drastic.Wires
A .NET 8.0 Port of Wires, Light binding library for Xamarin. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.5.7 | 152 | 6/6/2024 |