|
| 1 | +#if NET10_0_OR_GREATER |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +#else |
| 4 | +using System.Reflection; |
| 5 | +#endif |
| 6 | + |
| 7 | +namespace MudBlazor; |
| 8 | + |
| 9 | +internal static class MudBlazorEx |
| 10 | +{ |
| 11 | +#if NET10_0_OR_GREATER |
| 12 | + [UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "ToStringFast")] |
| 13 | + private static extern string MudToStringFast( |
| 14 | + [UnsafeAccessorType("MudBlazor.ColorExtensions, MudBlazor")] |
| 15 | + object _, |
| 16 | + Color color |
| 17 | + ); |
| 18 | + |
| 19 | + [UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "ToStringFast")] |
| 20 | + private static extern string MudToStringFast( |
| 21 | + [UnsafeAccessorType("MudBlazor.UnderlineExtensions, MudBlazor")] |
| 22 | + object _, |
| 23 | + Underline underline |
| 24 | + ); |
| 25 | + |
| 26 | + [UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "ToStringFast")] |
| 27 | + private static extern string MudToStringFast( |
| 28 | + [UnsafeAccessorType("MudBlazor.TypoExtensions, MudBlazor")] |
| 29 | + object _, |
| 30 | + Typo typo |
| 31 | + ); |
| 32 | +#else |
| 33 | + private static string MudToStringFast(object _, Color color) |
| 34 | + { |
| 35 | + var extensions = typeof(Color).Assembly.GetType("MudBlazor.ColorExtensions"); |
| 36 | + var method = extensions?.GetMethod("ToStringFast", types: [typeof(Color)], bindingAttr: BindingFlags.Public | BindingFlags.Static); |
| 37 | + return (string?)method?.Invoke(null, [color]) ?? throw new InvalidOperationException("Unable to find Color from MudBlazor"); |
| 38 | + } |
| 39 | + |
| 40 | + private static string MudToStringFast(object _, Underline underline) |
| 41 | + { |
| 42 | + var extensions = typeof(Underline).Assembly.GetType("MudBlazor.UnderlineExtensions"); |
| 43 | + var method = extensions?.GetMethod("ToStringFast", types: [typeof(Underline)], bindingAttr: BindingFlags.Public | BindingFlags.Static); |
| 44 | + return (string?)method?.Invoke(null, [underline]) ?? throw new InvalidOperationException("Unable to find Underline from MudBlazor"); |
| 45 | + } |
| 46 | + |
| 47 | + private static string MudToStringFast(object _, Typo typo) |
| 48 | + { |
| 49 | + var extensions = typeof(Typo).Assembly.GetType("MudBlazor.TypoExtensions"); |
| 50 | + var method = extensions?.GetMethod("ToStringFast", types: [typeof(Typo)], bindingAttr: BindingFlags.Public | BindingFlags.Static); |
| 51 | + return (string?)method?.Invoke(null, [typo]) ?? throw new InvalidOperationException("Unable to find Typo from MudBlazor"); |
| 52 | + } |
| 53 | +#endif |
| 54 | + |
| 55 | + public static string ToStringFast(this Color color) |
| 56 | + => MudToStringFast(null!, color); |
| 57 | + |
| 58 | + public static string ToStringFast(this Underline underline) |
| 59 | + => MudToStringFast(null!, underline); |
| 60 | + |
| 61 | + public static string ToStringFast(this Typo typo) |
| 62 | + => MudToStringFast(null!, typo); |
| 63 | +} |
0 commit comments