Better way to get properties from MenuItemDefinition.CustomData












0















I'm using customData property of the MenuItemDefinition from the ASP.NETBoilerplate Navigation to define some additional properties.



I'm able to access the properties using the following code



 var properties = menuItem.CustomData?.GetType().GetProperties();
bool isMegaMenu = (bool)properties.First(x => x.Name == "MegaMenu").GetValue(menuItem.CustomData);


This works fine but is there a better way to access the properties.
I have tried to access the object by casting it to a dynamic first



 dynamic t = menuItem.CustomData;
isMegaMenu = t.MegaMenu;


But that produces the following error



RuntimeBinderException: 'object' does not contain a definition for 'MegaMenu'


I have looked at this post which suggests to use InternalsVisibleTo but he uses it on a test case, i'm not sure it's a good idea to use it on production.



EDIT:



Here is what the MenuItemDefinition and the customData property looks like



new MenuItemDefinition(
PageNames.Apps,
L("Apps"),
url: "",
icon: "flaticon-paper-plane",
customData: new { MegaMenu = true }
)









share|improve this question

























  • Show your CustomData class.

    – aaron
    Nov 18 '18 at 1:59











  • @aaron I have added it to the question

    – Digvijayad
    Nov 18 '18 at 2:32











  • I could not reproduce. Put a breakpoint on isMegaMenu = t.MegaMenu; and check the value of t.

    – aaron
    Nov 18 '18 at 2:39











  • it shows that t has value of {MegaMenu = true} .

    – Digvijayad
    Nov 18 '18 at 3:13











  • The first t, yes. Check every t.

    – aaron
    Nov 18 '18 at 6:32
















0















I'm using customData property of the MenuItemDefinition from the ASP.NETBoilerplate Navigation to define some additional properties.



I'm able to access the properties using the following code



 var properties = menuItem.CustomData?.GetType().GetProperties();
bool isMegaMenu = (bool)properties.First(x => x.Name == "MegaMenu").GetValue(menuItem.CustomData);


This works fine but is there a better way to access the properties.
I have tried to access the object by casting it to a dynamic first



 dynamic t = menuItem.CustomData;
isMegaMenu = t.MegaMenu;


But that produces the following error



RuntimeBinderException: 'object' does not contain a definition for 'MegaMenu'


I have looked at this post which suggests to use InternalsVisibleTo but he uses it on a test case, i'm not sure it's a good idea to use it on production.



EDIT:



Here is what the MenuItemDefinition and the customData property looks like



new MenuItemDefinition(
PageNames.Apps,
L("Apps"),
url: "",
icon: "flaticon-paper-plane",
customData: new { MegaMenu = true }
)









share|improve this question

























  • Show your CustomData class.

    – aaron
    Nov 18 '18 at 1:59











  • @aaron I have added it to the question

    – Digvijayad
    Nov 18 '18 at 2:32











  • I could not reproduce. Put a breakpoint on isMegaMenu = t.MegaMenu; and check the value of t.

    – aaron
    Nov 18 '18 at 2:39











  • it shows that t has value of {MegaMenu = true} .

    – Digvijayad
    Nov 18 '18 at 3:13











  • The first t, yes. Check every t.

    – aaron
    Nov 18 '18 at 6:32














0












0








0








I'm using customData property of the MenuItemDefinition from the ASP.NETBoilerplate Navigation to define some additional properties.



I'm able to access the properties using the following code



 var properties = menuItem.CustomData?.GetType().GetProperties();
bool isMegaMenu = (bool)properties.First(x => x.Name == "MegaMenu").GetValue(menuItem.CustomData);


This works fine but is there a better way to access the properties.
I have tried to access the object by casting it to a dynamic first



 dynamic t = menuItem.CustomData;
isMegaMenu = t.MegaMenu;


But that produces the following error



RuntimeBinderException: 'object' does not contain a definition for 'MegaMenu'


I have looked at this post which suggests to use InternalsVisibleTo but he uses it on a test case, i'm not sure it's a good idea to use it on production.



EDIT:



Here is what the MenuItemDefinition and the customData property looks like



new MenuItemDefinition(
PageNames.Apps,
L("Apps"),
url: "",
icon: "flaticon-paper-plane",
customData: new { MegaMenu = true }
)









share|improve this question
















I'm using customData property of the MenuItemDefinition from the ASP.NETBoilerplate Navigation to define some additional properties.



I'm able to access the properties using the following code



 var properties = menuItem.CustomData?.GetType().GetProperties();
bool isMegaMenu = (bool)properties.First(x => x.Name == "MegaMenu").GetValue(menuItem.CustomData);


This works fine but is there a better way to access the properties.
I have tried to access the object by casting it to a dynamic first



 dynamic t = menuItem.CustomData;
isMegaMenu = t.MegaMenu;


But that produces the following error



RuntimeBinderException: 'object' does not contain a definition for 'MegaMenu'


I have looked at this post which suggests to use InternalsVisibleTo but he uses it on a test case, i'm not sure it's a good idea to use it on production.



EDIT:



Here is what the MenuItemDefinition and the customData property looks like



new MenuItemDefinition(
PageNames.Apps,
L("Apps"),
url: "",
icon: "flaticon-paper-plane",
customData: new { MegaMenu = true }
)






c# object c#-4.0 asp.net-core aspnetboilerplate






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 2:31







Digvijayad

















asked Nov 18 '18 at 1:39









DigvijayadDigvijayad

375410




375410













  • Show your CustomData class.

    – aaron
    Nov 18 '18 at 1:59











  • @aaron I have added it to the question

    – Digvijayad
    Nov 18 '18 at 2:32











  • I could not reproduce. Put a breakpoint on isMegaMenu = t.MegaMenu; and check the value of t.

    – aaron
    Nov 18 '18 at 2:39











  • it shows that t has value of {MegaMenu = true} .

    – Digvijayad
    Nov 18 '18 at 3:13











  • The first t, yes. Check every t.

    – aaron
    Nov 18 '18 at 6:32



















  • Show your CustomData class.

    – aaron
    Nov 18 '18 at 1:59











  • @aaron I have added it to the question

    – Digvijayad
    Nov 18 '18 at 2:32











  • I could not reproduce. Put a breakpoint on isMegaMenu = t.MegaMenu; and check the value of t.

    – aaron
    Nov 18 '18 at 2:39











  • it shows that t has value of {MegaMenu = true} .

    – Digvijayad
    Nov 18 '18 at 3:13











  • The first t, yes. Check every t.

    – aaron
    Nov 18 '18 at 6:32

















Show your CustomData class.

– aaron
Nov 18 '18 at 1:59





Show your CustomData class.

– aaron
Nov 18 '18 at 1:59













@aaron I have added it to the question

– Digvijayad
Nov 18 '18 at 2:32





@aaron I have added it to the question

– Digvijayad
Nov 18 '18 at 2:32













I could not reproduce. Put a breakpoint on isMegaMenu = t.MegaMenu; and check the value of t.

– aaron
Nov 18 '18 at 2:39





I could not reproduce. Put a breakpoint on isMegaMenu = t.MegaMenu; and check the value of t.

– aaron
Nov 18 '18 at 2:39













it shows that t has value of {MegaMenu = true} .

– Digvijayad
Nov 18 '18 at 3:13





it shows that t has value of {MegaMenu = true} .

– Digvijayad
Nov 18 '18 at 3:13













The first t, yes. Check every t.

– aaron
Nov 18 '18 at 6:32





The first t, yes. Check every t.

– aaron
Nov 18 '18 at 6:32












1 Answer
1






active

oldest

votes


















0














This looks like a job for the Dictionary.



In navigation provider:



customData: new Dictionary<string, object>() { { "MegaMenu", true }, {"AnotherKey", "AnotherData"} }


Then it can be safely consumed in view as



var customData = menuItem.CustomData as Dictionary<string, object>;

if (customData != null && customData.TryGetValue("MegaMenu", out var megaMenu) && (bool)megaMenu)
{
// MegaMenu true HTML
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357182%2fbetter-way-to-get-properties-from-menuitemdefinition-customdata%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This looks like a job for the Dictionary.



    In navigation provider:



    customData: new Dictionary<string, object>() { { "MegaMenu", true }, {"AnotherKey", "AnotherData"} }


    Then it can be safely consumed in view as



    var customData = menuItem.CustomData as Dictionary<string, object>;

    if (customData != null && customData.TryGetValue("MegaMenu", out var megaMenu) && (bool)megaMenu)
    {
    // MegaMenu true HTML
    }





    share|improve this answer




























      0














      This looks like a job for the Dictionary.



      In navigation provider:



      customData: new Dictionary<string, object>() { { "MegaMenu", true }, {"AnotherKey", "AnotherData"} }


      Then it can be safely consumed in view as



      var customData = menuItem.CustomData as Dictionary<string, object>;

      if (customData != null && customData.TryGetValue("MegaMenu", out var megaMenu) && (bool)megaMenu)
      {
      // MegaMenu true HTML
      }





      share|improve this answer


























        0












        0








        0







        This looks like a job for the Dictionary.



        In navigation provider:



        customData: new Dictionary<string, object>() { { "MegaMenu", true }, {"AnotherKey", "AnotherData"} }


        Then it can be safely consumed in view as



        var customData = menuItem.CustomData as Dictionary<string, object>;

        if (customData != null && customData.TryGetValue("MegaMenu", out var megaMenu) && (bool)megaMenu)
        {
        // MegaMenu true HTML
        }





        share|improve this answer













        This looks like a job for the Dictionary.



        In navigation provider:



        customData: new Dictionary<string, object>() { { "MegaMenu", true }, {"AnotherKey", "AnotherData"} }


        Then it can be safely consumed in view as



        var customData = menuItem.CustomData as Dictionary<string, object>;

        if (customData != null && customData.TryGetValue("MegaMenu", out var megaMenu) && (bool)megaMenu)
        {
        // MegaMenu true HTML
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 '18 at 3:40









        XeevisXeevis

        2,2931418




        2,2931418






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357182%2fbetter-way-to-get-properties-from-menuitemdefinition-customdata%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()