问题描述
我对C#中的动态特性(),尤其是因为在我的代码库的某些部分我用了很多反思
我的问题是双重的:
1。没有动态代替仿制药,如下面的情况下
泛型方法:
公共静态无效Do_Something_If_Object_Not_Null< SOMETYPE>(SOMETYPE ObjToTest){
//测试对象不为空,而无论其类型
如果(EqualityComparer<的!; SOMETYPE> .Default.Equals(ObjToTest,默认(SOMETYPE))){
//做些什么
}
}
动态方法(??):
公共静态无效Do_Something_If_Object_Not_Null(动态ObjToTest){
//测试对象不为空,不管它的类型?但如何?
如果(ObjToTest!= NULL){
//做些什么
}
}
2。没有动态现在允许方法返回匿名类型,如下面的情况:
公共静态清单<动态> ReturnAnonymousType(){
返回MyDataContext.SomeEntities.Entity.Select(E => e.Property1,e.Property2).ToList();
}
酷,欢呼声
编辑:
已经通过我的问题想多一点,并在答案的灯光下,我看到我彻底搞砸了主通用/动态问题。他们的确是完全不同的。所以,是的,感谢所有的信息。
约2点,但?
动态
可能简化反射场景数量有限(你知道成员的名字前面,但没有接口) - 特别是,它可能与一般的运营商帮助() - 但比一般的经营诀窍等,很少有。交叉泛型
泛型允许你知道(在编译时)你正在使用的类型 - 相反,动态
没有的保养的有关类型。
特别 - 泛型允许您指定并证明了一些关于的一类条件的 - 即它可能实现一些接口或具有公共参数的构造函数。 动态
不帮之一:它不支持接口,并且不是简单的不关心的接口糟糕的是,这意味着我们甚至不能查看显式接口实现与动态
。
此外,动态
真的是对象
的一个特例,所以拳击进场,但有vengence。
在现实中,你应该将动态
的使用限制在少数情况下:
- COM互操作
- DLR互操作
- 的也许的一些光鸭打字
- 也许的一些通用的运营商
有关所有其他情况下,仿制药和普通C#是要走的路。
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"动态"关键字使泛型多余?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!