Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from anonymous type

Tags:

I have a method as following:

public void MyMethod(object obj){    // implement  } 

And I call it like this:

MyMethod(new { myparam= "waoww"}); 

So how can I implement MyMethod() to get myparam value?

Edit

I use this:

dynamic d= obj;  string param = d.myparam;  

but the error rise :

'object' does not contain a definition for 'myparam'  

also I use breakpoint and I see the d have myparam string property.

And is there any way to check dynamic type to if contain any property like this:

if(d.contain(myparam))? 

Edit II

This is my main code:

public static MvcHtmlString SecureActionLink(this HtmlHelper htmlHelper,           string linkText, string actionName, string controllerName,           object routeValues, object htmlAttributes) {       string areaName =           (string)htmlHelper.ViewContext.RouteData.DataTokens["area"];          dynamic areaObject = routeValues;          if(areaObject != null && !string.IsNullOrEmpty(areaObject.area))             areaName = areaObject.area;  // more } 

and call it as:

<p>@Html.SecureActionLink("Secure Link between Areas", "Index", "Context",                            new { area = "Settings" }, null)</p> 

And Error is:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a  definition for 'area'   Line 303:  dynamic areaObject = routeValues;  Line 304:  Line 305:  if(areaObject != null && !string.IsNullOrEmpty(areaObject.area))  Line 306:      areaName = areaObject.area;  Line 307:   Source File: D:\Projects\MyProject\HtmlHelpers\LinkExtensions.cs    Line: 305  

Edit III

This is my AssemblyInfo of HtmlHelper definition:

[assembly: AssemblyTitle("MyProject.Presentation")] [assembly: InternalsVisibleTo("cpanel.MyProject.dev")] 

but there is an error yet: 'object' does not contain a definition for 'area' I use different assemblies but how can it possible, when I use breakpoint I can see that my dynamic areaobject have area name property and also I can see the value of that, but the error say: 'object' does not contain a definition for 'area' I can't figure it how it can be possible?

Edit

I change the assembly and now dynamic type is internal but the error remains as before

like image 815
Saeid Avatar asked Mar 17 '12 12:03

Saeid


People also ask

What is an anonymous type in C#?

Anonymous types in C# are the types which do not have a name or you can say the creation of new types without defining them. It is introduced in C# 3.0. It is a temporary data type which is inferred based on the data that you insert in an object initializer.

What is the difference between an anonymous type and a regular data type?

The compiler gives them a name although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object.

How do I make an anonymous object?

You create anonymous types by using the new operator together with an object initializer. For more information about object initializers, see Object and Collection Initializers. The following example shows an anonymous type that is initialized with two properties named Amount and Message .


1 Answers

Use this one:

string area = areaObject.GetType().GetProperty("area").GetValue(areaObject, null); 
like image 119
Saeid Avatar answered Oct 19 '22 03:10

Saeid