在对象转换匿名类型和检索一个场 [英] Cast an Anonymous Types in Object and retrieve one Field
问题描述
我用C#ASP.NET4。
我来用匿名类型(字段:标题,类别ID),中继器的方法,直放站里面我还放置了标签:
VAR parentCategories从context.CmsCategories C =
其中,c.CategoryNodeLevel == 1
选择新的{c.Title,c.CategoryId};
uxRepeter.DataSource = parentCategories;
uxRepeter.DataBind();
我需要改变文本属性为每个标签我对直放站事件的ItemDataBound
里面直放站 保护无效uxRepeter_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
超链接链接=(超链接)e.Item.FindControl(uxLabel);
uxLabel.Text = //如何在这里做!!!!!!!!
}
所以,我需要设置使用e.Item(或任何一个更好的办法)为Label.Text的属性。
我的问题,我不能投e.Item(匿名类型字段标题),并将其设置为文本礼我的标签。
我明白匿名类型可强制转换为唯一对象类型,但对我来说我的匿名类型都有标题和类别ID字段。
我的问题:
如何投放和检索字段与我感兴趣?感谢您的时间呢?
编辑:
某些错误我收到:
无法投类型的对象'<> f__AnonymousType0`2 [System.String,System.Int32]'键入'System.String'。
选项约瑟夫presents是好的,但也有是的你可以做这个可怕的方式。这有点脆弱,因为它依赖于你的究竟的同样的方式在两地指定匿名类型。在这里,我们去:
公共静态牛逼CastByExample< T>(对象输入,T为例)
{
回报(T)输入;
}
然后:
对象项目= ...; //但是你从控制值//指定了榜样使用相同的属性名称,类型和顺序
//其他地方一样。
VAR投= CastByExample(项目,新{标题=默认(串)
类别ID =默认值(INT)});
VAR的结果= cast.Title;
编辑:进一步的皱纹 - 两个匿名类型创建前pressions必须在同一个组件(项目)。对不起,我忘了提,现在才
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:
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 Text Properties for each label inside my Repeater on Repeater Event ItemDataBound
protected void uxRepeter_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
HyperLink link = (HyperLink)e.Item.FindControl("uxLabel");
uxLabel.Text = // How to do here!!!!!!!!
}
So I need set the properties for Label.Text using e.Item (or a better way if any).
My problem I'm not able to CAST the e.Item (Anonymous type Field Title) and set it as Text Propriety for my Label.
I understand Anonymous Type can be casted to only Object Type, but in my case my Anonymous Type has Title and CategoryId Fields.
My question:
How to cast and retrieve the field with I'm interested? Thanks for your time on this?
EDIT: SOME ERROR I RECEIVE:
Unable to cast object of type '<>f__AnonymousType0`2[System.String,System.Int32]' to type 'System.String'.
The options Joseph presents are good ones, but there is a horrible way you can do this. It's somewhat fragile, as it relies on you specifying the 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 - the two anonymous type creation expressions have to be in the same assembly (project). Sorry for forgetting to mention that before now.
这篇关于在对象转换匿名类型和检索一个场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!