Retrieve Svg Icon Dynamically by String Name

1 Answer 20 Views
Drawer
John
Top achievements
Rank 1
John asked on 05 Sep 2025, 09:20 PM

Does anyone know of a way to dynamically retrieve an SvgIcon from Telerik.SvgIcons by the string name?

I am trying to assign icons to drawer items from looking up string names in a database. I can create an interface that has a custom dictionary that maps strings to icons, but before I did that, I wanted to make sure that there wasn't something already available that I am missing.

 

NiV-L-A
Top achievements
Rank 2
Iron
commented on 09 Sep 2025, 04:11 PM

1 Answer, 1 is accepted

Sort by
0
Tsvetomir
Telerik team
answered on 10 Sep 2025, 12:01 PM

Hello John,

There are two main approaches to achieving dynamic icon assignment:

1. Reflection (Dynamic Lookup) - As mentioned in Roberto's answer, you can use reflection to resolve an SvgIcon at runtime using its string name. This is useful when you want maximum flexibility and do not want to maintain a static mapping. Example:

using System.Reflection;
using Telerik.SvgIcons;

public static object? GetSvgIconByName(string iconName)
{
    var property = typeof(SvgIcon).GetProperty(iconName, BindingFlags.Public | BindingFlags.Static);
    return property?.GetValue(null);
}

Pass your database string to this method to get the corresponding icon.

2. Dictionary Mapping (Static Lookup) - Alternatively, you can create a dictionary that maps string names to SvgIcon instances. This approach is faster and allows for custom handling of missing icons or aliases, but requires manual maintenance:

using Telerik.SvgIcons;

var iconMap = new Dictionary<string, object>
{
    { "Accessibility", SvgIcon.Accessibility },
    { "Add", SvgIcon.Add },
    // Add more mappings as needed
};

if (iconMap.TryGetValue(iconNameFromDb, out var icon))
{
    // Use icon
}

Summary

  • Use reflection for flexibility and dynamic scenarios.
  • Use a dictionary for better performance and control if your icon set is fixed or well-known.

These are the available approaches, so you can choose the one that best fits your requirements. I hope this serves you well.

    Regards,
    Tsvetomir
    Progress Telerik

    Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

    Tags
    Drawer
    Asked by
    John
    Top achievements
    Rank 1
    Answers by
    Tsvetomir
    Telerik team
    Share this question
    or