Cast an Anonymous Types in Object and retrieve one Field

前端 未结 4 1924
闹比i
闹比i 2020-12-03 23:18

I use C# asp.net4.

I have a method to populate a Repeater with Anonymous Types (Fields: Title, CategoryId), inside the Repeater I also placed a Label:



        
4条回答
  •  暖寄归人
    2020-12-04 00:13

    You can use a dynamic in this case. I think the code would 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's existence, until runtime.
    }
    

提交回复
热议问题