使用动态类型从匿名对象是不好的做法得到值? [英] Is using dynamic types to get values from anonymous objects bad practice?

查看:139
本文介绍了使用动态类型从匿名对象是不好的做法得到值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我的问题是不是ASP.Net

Note: My question is not about ASP.Net.

我有使用LINQ匿名收集绑定的GridView

I have a GridView bound with an anonymous collection using LINQ.

我想在网格中的一个事件处理程序获得的约束对象,它是不能够转换为任何静态类型,因为它的类型是匿名的值。

I want in an event handler in the grid get a value from the bound object which is not able to cast to any static type because its type is anonymous.

要解决我用动态类型来获取值这个问题。

To solve this problem I used dynamic type to get the value

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        dynamic dataItem = e.Row.DataItem; // getting the bound object

        bool isCurrentEntity = dataItem.IsCurrentEntity;
        if (isCurrentEntity)
            e.Row.Style.Add(HtmlTextWriterStyle.FontWeight, "Bold");
    }
}



我的问题是,在这里使用动态类型一种不好的做法?

My question is that is using the dynamic type here is a bad practice?

推荐答案

我会说这就是一种不好的做法,是的。如果您想使用在多个地方匿名类型的代码,然后它是重要的,足以使其成为名义上的类型。记住,动态的再次的启动编译器,这是一个严重的性能成本,而且它的换方便显影剂换取隐藏错误由用户的,这是被发现严重的客户关系成本。

I would call that a bad practice, yes. If you want to use the anonymous type in multiple places in the code then it is important enough to make it a nominal type. Remember, dynamic starts the compiler again, which is a serious performance cost, and it trades convenience for the developer in exchange for hiding bugs to be discovered by the user, which is a serious customer relations cost.

动态旨在使代码是的将是缓慢和脆弱更愉快的阅读。如果你已经打算使用反射,或者已经将要谈论一个传统的COM自动化对象,那么你的的将是在缓慢的,危险的代码的世界。它很可能会成为易于读取慢危险的代码。如果你能避免在这个世界之中,做到这一点;使用定式。

Dynamic was designed to make code that was already going to be slow and brittle more pleasant to read. If you were already going to be using reflection, or already going to be talking to a legacy COM automation object, then you were already going to be in a world of slow, dangerous code. It might as well be easy-to-read slow dangerous code. If you can avoid being in that world, do it; use a nominal type.

另外,如果你想使用匿名类型的的有它被静态类型,使用举例投'招:

Alternatively, if you want to use an anonymous type and have it be statically typed, use the 'cast by example' trick:

object anon;
void M1()
{
    anon = new { X = 123, Y = 456 };
}
void M2()
{ 
    // we want to get anon.X, but it is anonymous. How do we
    // trick the compiler into it?

    var cast = CastByExample(new { X = 0, Y = 0 }, anon);
    int x = cast.X;  // gets anon.X!
}

static T CastByExample<T>(T example, object ob) where T : class
{
    return (T)ob;
}



偷偷摸摸的,不是吗?举例招演员使用泛型类型推断说:我认为这个对象在这里有相同的匿名类型的结构,因为这一个在这里。

Sneaky, eh? The cast by example trick uses generic type inference to say "I think this object over here has the same anonymous type structure as this one here".

注意施放例子的只能在同一装配的。您不能例如在一个装配创建成另一种创建一个匿名类型匿名类型转换。

Note that cast by example only works in the same assembly. You cannot cast by example an anonymous type created in one assembly into an anonymous type created in another.

这篇关于使用动态类型从匿名对象是不好的做法得到值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆