C#4.0“动态”关键字make泛型多余? [英] Does the C# 4.0 "dynamic" keyword make Generics redundant?

查看:104
本文介绍了C#4.0“动态”关键字make泛型多余?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴C#中的动态功能( C#4动态关键字 - 为什么不是?),特别是因为在我的代码的某些Library部分我使用了很多反思。



我的问题有两个:



1。是否动态替代泛型,如以下情况?



泛型方法:

  public static void Do_Something_If_Object_Not_Null< SomeType>(SomeType ObjToTest){

//测试对象不为空,无论其类型为
if(!EqualityComparer< SomeType> .Default.Equals(ObjToTest,default(SomeType))){
// do something
}
}

动态方法(?):

  public static void Do_Something_If_Object_Not_Null ObjToTest){

//测试对象不为null,不管其类型?但是怎么样?
if(ObjToTest!= null){
// do something
}
}

2。 动态现在允许方法返回匿名类型,如下例所示:

  public static列表与LT;动态> ReturnAnonymousType(){
return MyDataContext.SomeEntities.Entity.Select(e => e.Property1,e.Property2).ToList();
}

cool,cheers



编辑:



再次考虑了我的问题,并根据答案,我看到我完全搞砸了主通用/动态问题。他们确实完全不一样。那么是的,谢谢所有的信息。



点2如何?

解决方案

动态可能会简化有限数量的反射方案(您知道前面的成员名称,但没有接口) - 特别是它可能帮助通用运算符(虽然其他答案存在) - 但除了通用运算符通用代码允许您知道(在编译时)您正在使用的类型 - 相反,



关于该类型的动态不关心。
特别是 - 泛型允许你指定和证明一些关于类型的条件,即它可以实现一些接口,或者有一个public parameterless构造函数。 动态无助于:它不支持接口,而不仅仅是不关心接口,这意味着我们甚至不能看到显式接口实现与动态



另外,动态真的是一个特殊情况,对象,所以拳击发挥,但有一个vengence。



实际上,您应该将动态的使用限制在少数情况下:




  • COM interop

  • DLR interop

  • 可能一些浅鸭打字

  • 可能一些通用运算符



对于所有其他情况,泛型和常规C#都是要走的路。 p>

I'm very excited about the dynamic features in C# (C#4 dynamic keyword - why not?), especially because in certain Library parts of my code I use a lot of reflection.

My question is twofold:

1. does "dynamic" replace Generics, as in the case below?

Generics method:

public static void Do_Something_If_Object_Not_Null<SomeType>(SomeType ObjToTest) {

        //test object is not null, regardless of its Type
        if (!EqualityComparer<SomeType>.Default.Equals(ObjToTest, default(SomeType))) {
            //do something
        }
    }

dynamic method(??):

public static void Do_Something_If_Object_Not_Null(dynamic ObjToTest) {

        //test object is not null, regardless of its Type?? but how?
        if (ObjToTest != null) {
            //do something
        }
    }

2. does "dynamic" now allow for methods to return Anonymous types, as in the case below?:

 public static List<dynamic> ReturnAnonymousType() {
        return MyDataContext.SomeEntities.Entity.Select(e => e.Property1, e.Property2).ToList();
    }

cool, cheers

EDIT:

Having thought through my question a little more, and in light of the answers, I see I completely messed up the main generic/dynamic question. They are indeed completely different. So yeah, thanks for all the info.

What about point 2 though?

解决方案

dynamic might simplify a limited number of reflection scenarios (where you know the member-name up front, but there is no interface) - in particular, it might help with generic operators (although other answers exist) - but other than the generic operators trick, there is little crossover with generics.

Generics allow you to know (at compile time) about the type you are working with - conversely, dynamic doesn't care about the type. In particular - generics allow you to specify and prove a number of conditions about a type - i.e. it might implement some interface, or have a public parameterless constructor. dynamic doesn't help with either: it doesn't support interfaces, and worse than simply not caring about interfaces, it means that we can't even see explicit interface implementations with dynamic.

Additionally, dynamic is really a special case of object, so boxing comes into play, but with a vengence.

In reality, you should limit your use of dynamic to a few cases:

  • COM interop
  • DLR interop
  • maybe some light duck typing
  • maybe some generic operators

For all other cases, generics and regular C# are the way to go.

这篇关于C#4.0“动态”关键字make泛型多余?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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