dimanche 20 juin 2021

How to get all methods and their return values in .NET Core? [duplicate]

I have a static class like this

public static partial class PublicWidgetZones
{
    public static string AccountNavigationAfter => "account_navigation_after";
    public static string AccountNavigationBefore => "account_navigation_before";
    public static string AdminHeaderLinksAfter => "admin_header_links_after";
}

I want to write a dynamic program that returns a Dictionary<string, string> that has these values:

      Key                                          Value
------------------------------------------------------------------------
"AccountNavigationAfter"                    "account_navigation_after"
"AccountNavigationBefore"                    "account_navigation_before"
"AdminHeaderLinksAfter "                     "admin_header_links_after"

N.B: I have a program that return only methods name

    public static List<string> GetWidgetZoneNames(Type type)
    {
        List<string> names = new List<string>();
        foreach (var method in type.GetMethods())
                    {    
                        string[] splitedMethodName = method.Name.Split(new string[] { "get_" }, StringSplitOptions.None);
                        if (splitedMethodName.Length > 1)
                        {
                            names.Add(splitedMethodName[1]);
                        }
                    }

           return names;
    }




Aucun commentaire:

Enregistrer un commentaire