Better way to get properties from MenuItemDefinition.CustomData
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
|
show 2 more comments
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
Show yourCustomData
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 onisMegaMenu = t.MegaMenu;
and check the value oft
.
– aaron
Nov 18 '18 at 2:39
it shows thatt
has value of{MegaMenu = true}
.
– Digvijayad
Nov 18 '18 at 3:13
The firstt
, yes. Check everyt
.
– aaron
Nov 18 '18 at 6:32
|
show 2 more comments
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
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
c# object c#-4.0 asp.net-core aspnetboilerplate
edited Nov 18 '18 at 2:31
Digvijayad
asked Nov 18 '18 at 1:39
DigvijayadDigvijayad
375410
375410
Show yourCustomData
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 onisMegaMenu = t.MegaMenu;
and check the value oft
.
– aaron
Nov 18 '18 at 2:39
it shows thatt
has value of{MegaMenu = true}
.
– Digvijayad
Nov 18 '18 at 3:13
The firstt
, yes. Check everyt
.
– aaron
Nov 18 '18 at 6:32
|
show 2 more comments
Show yourCustomData
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 onisMegaMenu = t.MegaMenu;
and check the value oft
.
– aaron
Nov 18 '18 at 2:39
it shows thatt
has value of{MegaMenu = true}
.
– Digvijayad
Nov 18 '18 at 3:13
The firstt
, yes. Check everyt
.
– 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
|
show 2 more comments
1 Answer
1
active
oldest
votes
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
}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
}
add a comment |
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
}
add a comment |
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
}
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
}
answered Nov 18 '18 at 3:40
XeevisXeevis
2,2931418
2,2931418
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 oft
.– 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 everyt
.– aaron
Nov 18 '18 at 6:32