在对象中转换匿名类型并检索一个字段

发布于 2024-11-27 20:34:18 字数 1062 浏览 2 评论 0原文

我使用C# asp.net4。

我有一种用匿名类型(字段:Title、CategoryId)填充转发器的方法,在转发器内我还放置了一个标签:

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

我需要在转发器事件 ItemDataBound 上更改转发器内每个标签的文本属性,

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

因此我需要设置属性对于 Label.Text 使用 e.Item (或更好的方法,如果有的话)。

我的问题是我无法转换 e.Item(匿名类型字段标题)并将其设置为我的标签的文本属性。

我知道匿名类型只能转换为对象类型,但就我而言,我的匿名类型具有 Title 和 CategoryId 字段。

我的问题:

如何投射和检索我感兴趣的字段?感谢您抽出时间来讨论这个问题?

编辑: 我收到一些错误:

Unable to cast object of type '<>f__AnonymousType0`2[System.String,System.Int32]' to type 'System.String'.

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'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

画骨成沙 2024-12-04 20:34:18

约瑟夫提出的选择是好的,但是有一种可怕的方法可以做到这一点。它有点脆弱,因为它依赖于您在两个地方以完全相同的方式指定匿名类型。我们开始:

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

然后:

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;

编辑:进一步的皱纹 - 两个匿名类型创建表达式必须位于同一个程序集(项目)中。抱歉之前忘记提及这一点。

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.

聽兲甴掵 2024-12-04 20:34:18

您无法将匿名类型转换为任何类型,因为您实际上没有可以将其转换为的类型,正如您基本上已经指出的那样。

所以你确实有两个选择。

  1. 不要转换为匿名类型,而应该转换为您仅为处理此场景而构建的已知类型,或者
  2. 将动态变量分配给该项目并使用动态属性代替

示例 1:

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

示例 2:(我也认为您是最后一行你的意思是分配链接变量)

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
}

You can't cast the anonymous type to anything because you literally have no type to cast it to, as you've basically pointed out already.

So you really have two options.

  1. Don't cast to an anonymous type, but rather a known type that you build just for handling this scenario or
  2. assign a dynamic variable to the item 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: (also I think you're last line you meant to assign the link var)

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
}
得不到的就毁灭 2024-12-04 20:34:18

在这种情况下,您可以使用dynamic。我认为代码是:

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.
}

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.
}
不美如何 2024-12-04 20:34:18

您不能直接转换为 (typeof(new { Title = "", CategoryID = 0 })) 吗?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文