Skip to content

Commit df46be8

Browse files
authored
Add TryGetInstance (#3569)
1 parent 3e25fb1 commit df46be8

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4915,7 +4915,7 @@
49154915
</member>
49164916
<member name="T:Microsoft.FluentUI.AspNetCore.Components.CustomIcon">
49174917
<summary>
4918-
Custom icon loaded from <see cref="M:Microsoft.FluentUI.AspNetCore.Components.IconsExtensions.GetInstance(Microsoft.FluentUI.AspNetCore.Components.IconInfo)"/>
4918+
Custom icon loaded from <see cref="!:IconsExtensions.GetInstance(IconInfo)"/>
49194919
</summary>
49204920
</member>
49214921
<member name="M:Microsoft.FluentUI.AspNetCore.Components.CustomIcon.#ctor">
@@ -5125,11 +5125,12 @@
51255125
<member name="T:Microsoft.FluentUI.AspNetCore.Components.IconsExtensions">
51265126
<summary />
51275127
</member>
5128-
<member name="M:Microsoft.FluentUI.AspNetCore.Components.IconsExtensions.GetInstance(Microsoft.FluentUI.AspNetCore.Components.IconInfo)">
5128+
<member name="M:Microsoft.FluentUI.AspNetCore.Components.IconsExtensions.GetInstance(Microsoft.FluentUI.AspNetCore.Components.IconInfo,System.Nullable{System.Boolean})">
51295129
<summary>
51305130
Returns a new instance of the icon.
51315131
</summary>
51325132
<param name="icon">The <see cref="T:Microsoft.FluentUI.AspNetCore.Components.IconInfo"/> to instantiate.</param>
5133+
<param name="throwOnError">true to throw an exception if the type is not found (default); false to return null.</param>
51335134
<remarks>
51345135
This method requires dynamic access to code. This code may be removed by the trimmer.
51355136
If the assembly is not yet loaded, it will be loaded by the method `Assembly.Load`.
@@ -5138,6 +5139,19 @@
51385139
<returns></returns>
51395140
<exception cref="T:System.ArgumentException">Raised when the <see cref="P:Microsoft.FluentUI.AspNetCore.Components.IconInfo.Name"/> is not found in predefined icons.</exception>
51405141
</member>
5142+
<member name="M:Microsoft.FluentUI.AspNetCore.Components.IconsExtensions.TryGetInstance(Microsoft.FluentUI.AspNetCore.Components.IconInfo,Microsoft.FluentUI.AspNetCore.Components.CustomIcon@)">
5143+
<summary>
5144+
Tries to return a new instance of the icon.
5145+
</summary>
5146+
<param name="icon">The <see cref="T:Microsoft.FluentUI.AspNetCore.Components.IconInfo"/> to instantiate.</param>
5147+
<param name="result">When this method returns, contains the <see cref="T:Microsoft.FluentUI.AspNetCore.Components.CustomIcon"/> value if the conversion succeeded, or null if the conversion failed. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
5148+
<remarks>
5149+
This method requires dynamic access to code. This code may be removed by the trimmer.
5150+
If the assembly is not yet loaded, it will be loaded by the method `Assembly.Load`.
5151+
To avoid any issues, the assembly must be loaded before calling this method (e.g. adding an icon in your code).
5152+
</remarks>
5153+
<returns>True if the icon was found and created; otherwise, false.</returns>
5154+
</member>
51415155
<member name="M:Microsoft.FluentUI.AspNetCore.Components.IconsExtensions.GetAllIcons">
51425156
<summary>
51435157
Returns a new instance of the icon.

src/Core/Components/Icons/IconsExtensions.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static partial class IconsExtensions
1717
/// Returns a new instance of the icon.
1818
/// </summary>
1919
/// <param name="icon">The <see cref="IconInfo"/> to instantiate.</param>
20+
/// <param name="throwOnError">true to throw an exception if the type is not found (default); false to return null.</param>
2021
/// <remarks>
2122
/// This method requires dynamic access to code. This code may be removed by the trimmer.
2223
/// If the assembly is not yet loaded, it will be loaded by the method `Assembly.Load`.
@@ -25,7 +26,7 @@ public static partial class IconsExtensions
2526
/// <returns></returns>
2627
/// <exception cref="ArgumentException">Raised when the <see cref="IconInfo.Name"/> is not found in predefined icons.</exception>
2728
[RequiresUnreferencedCode("This method requires dynamic access to code. This code may be removed by the trimmer.")]
28-
public static CustomIcon GetInstance(this IconInfo icon)
29+
public static CustomIcon GetInstance(this IconInfo icon, bool? throwOnError = true)
2930
{
3031
var assemblyName = string.Format(LibraryName, icon.Variant);
3132
var assembly = GetAssembly(assemblyName);
@@ -49,7 +50,30 @@ public static CustomIcon GetInstance(this IconInfo icon)
4950
}
5051
}
5152

52-
throw new ArgumentException($"Icon 'Icons.{icon.Variant}.Size{(int)icon.Size}.{icon.Name}' not found.");
53+
if (throwOnError == true || throwOnError == null)
54+
{
55+
throw new ArgumentException($"Icon 'Icons.{icon.Variant}.Size{(int)icon.Size}.{icon.Name}' not found.");
56+
}
57+
58+
return default!;
59+
}
60+
61+
/// <summary>
62+
/// Tries to return a new instance of the icon.
63+
/// </summary>
64+
/// <param name="icon">The <see cref="IconInfo"/> to instantiate.</param>
65+
/// <param name="result">When this method returns, contains the <see cref="CustomIcon"/> value if the conversion succeeded, or null if the conversion failed. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
66+
/// <remarks>
67+
/// This method requires dynamic access to code. This code may be removed by the trimmer.
68+
/// If the assembly is not yet loaded, it will be loaded by the method `Assembly.Load`.
69+
/// To avoid any issues, the assembly must be loaded before calling this method (e.g. adding an icon in your code).
70+
/// </remarks>
71+
/// <returns>True if the icon was found and created; otherwise, false.</returns>
72+
[RequiresUnreferencedCode("This method requires dynamic access to code. This code may be removed by the trimmer.")]
73+
public static bool TryGetInstance(this IconInfo icon, out CustomIcon? result)
74+
{
75+
result = GetInstance(icon, throwOnError: false);
76+
return result != null;
5377
}
5478

5579
/// <summary>

0 commit comments

Comments
 (0)