Including anonymous types in an object and getting a single field

I am using C # asp.net4.

I have a method for filling the repeater with anonymous types (fields: name, category), inside the repeater I also placed a label:

var parentCategories = from c in context.CmsCategories where c.CategoryNodeLevel == 1 select new { c.Title, c.CategoryId }; uxRepeter.DataSource = parentCategories; uxRepeter.DataBind(); 

I need to change the text properties for each label inside my repeater on an Event Repeater ItemDataBound

  protected void uxRepeter_ItemDataBound(object sender, RepeaterItemEventArgs e) { HyperLink link = (HyperLink)e.Item.FindControl("uxLabel"); uxLabel.Text = // How to do here!!!!!!!! } 

Therefore, I need to set the properties for Label.Text using e.Item (or the best way, if any).

My problem: I cannot execute CAST e.Item (anonymous type header name) and set it as a text expression for my label.

I understand that an anonymous type can only be applied to an object type, but in my case, my anonymous type has Title and CategoryId fields.

My question is:

How to dump and retrieve a field with interest? Thanks for your time on this?

EDIT: SOME ERRORS RECEIVED:

 Unable to cast object of type '<>f__AnonymousType0`2[System.String,System.Int32]' to type 'System.String'. 
+5
source share
4 answers

Joseph's options are good, but there is a terrible way to do this. It is somewhat fragile because it relies on you specifying an anonymous type in exactly the same way in two places. Here we go:

 public static T CastByExample<T>(object input, T example) { return (T) input; } 

Then:

 object item = ...; // However you get the value from the control // Specify the "example" using the same property names, types and order // as elsewhere. var cast = CastByExample(item, new { Title = default(string), CategoryId = default(int) } ); var result = cast.Title; 

EDIT: Further wrinkle - two expressions for creating an anonymous type should be in the same assembly (project). Sorry to forget to mention this before.

+10
source

You cannot throw an anonymous type onto anything because you literally don't have a single type to give away, as you have basically pointed out.

So you really have two options.

  • Do not apply to an anonymous type, but rather to a known type that you create to handle this script or
  • assign a dynamic variable to an element and use dynamic properties instead

Example 1:

 var parentCategories = from c in context.CmsCategories where c.CategoryNodeLevel == 1 select new RepeaterViewModel { c.Title, c.CategoryId }; 

Example 2: (I also think that you are the last line that you would like to assign to the var link)

 protected void uxRepeter_ItemDataBound(object sender, RepeaterItemEventArgs e) { HyperLink link = (HyperLink)e.Item.FindControl("uxLabel"); dynamic iCanUseTitleHere = e.Item; link.Text = iCanUseTitleHere.Title; //no compilation issue here } 
+4
source

In this case, you can use dynamic . I think the code will be:

 protected void uxRepeter_ItemDataBound(object sender, RepeaterItemEventArgs e) { dynamic link = (dynamic)e.Item.FindControl("uxLabel"); uxLabel.Text = link.Title; //since 'link' is a dynamic now, the compiler won't check for the Title property existence, until runtime. } 
+4
source

Can't you just click (typeof(new { Title = "", CategoryID = 0 })) ?

0
source

Source: https://habr.com/ru/post/948785/