不再有动态的、尴尬的反思了吗?

发布于 2024-07-18 06:05:15 字数 98 浏览 3 评论 0原文

C# 4.0 引入了 dynamic 关键字,它将在运行时查找。

这是否意味着我们不再需要尴尬的反思? 如果是的话,你能举个例子吗?

C# 4.0 introduces dynamic keyword, which will look up at run-time.

Does this mean we'll need awkward reflection no more? If does, Can you show up an example of it?

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

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

发布评论

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

评论(3

氛圍 2024-07-25 06:05:15

我们仍然会使用反射 - 对常规 CLR 对象使用“动态”将调用基于反射的调度程序。

所以 - 我们仍然会有反射,但它会更容易做到。

这是一个例子:

// Via 'dynamic'    
dynamic dx = GetSomeCLRObject();
dx.DoSomething();
dx.SomeMember = 2;

// Via Reflection
object x = GetSomeCLRObject();
Type xt = x.GetType();
MemberInfo DoSomethingMethod = xt.GetMethod("DoSomething");
DoSomethingMethod.Invoke(x, null);
PropertyInfo SomeMemberProperty = xt.GetProperty("SomeMember");
SomeMemberProperty.SetValue(x, 2);

我不了解你,但我喜欢前者。 =)

在这两种情况下,我都没有得到编译时检查、没有 Intellisense、没有 IDE 支持 - 但前一种情况比后一种情况更具表现力。

We'll still have Reflection - using 'dynamic' against regular CLR objects will invoke a Reflection-based dispatcher.

So - we'll still have Reflection, but it'll be easier to do.

Here's an example:

// Via 'dynamic'    
dynamic dx = GetSomeCLRObject();
dx.DoSomething();
dx.SomeMember = 2;

// Via Reflection
object x = GetSomeCLRObject();
Type xt = x.GetType();
MemberInfo DoSomethingMethod = xt.GetMethod("DoSomething");
DoSomethingMethod.Invoke(x, null);
PropertyInfo SomeMemberProperty = xt.GetProperty("SomeMember");
SomeMemberProperty.SetValue(x, 2);

I don't know about you, but I like the former. =)

In both these cases, I get no compile-time checking, no Intellisense, no IDE support - but the former case is much more expressive than the latter.

简美 2024-07-25 06:05:15

动态调度只是反射的一种可能用途。 有很多充分的理由询问类的结构,获取有关该结构的信息并以某种形式可视化或以某种方式对其进行操作,而无需动态访问成员。 反思将继续存在。 :)

如果您想要动态关键字的示例,这里有一段来自 PDC 的视频 < a href="http://www.microsoft.com/presspass/exec/techfellow/hejlsberg/default.mspx" rel="nofollow noreferrer">该人本人谈论它(以及其他与 C# 4.0 相关的内容) )。

Dynamic dispatch is only one possible use of Reflection. There are many good reasons to interrogate a class for its structure, get information about that structure and visualize in some form or act on it in some way without ever dynamically accessing members. Reflection is here to stay. :)

If you want examples of the dynamic keyword, here is a video from PDC of the man himself talking about it (and other stuff C# 4.0 related).

青衫儰鉨ミ守葔 2024-07-25 06:05:15

动态将大大有助于解决仅通过名称已知的方法的问题,其中该名称是已知的并在编译时固定 - 但当然,如果您控制类型,此类方法也可以表示为接口。

在某些情况下,动态根本没有帮助:

  • 方法名称在编译时未知(即从配置/用户输入加载)
  • 对象创建
  • 可能是一些泛型场景

最大的用途是对于动态,请参阅:

  • COM 互操作(显然)
  • 通用运算符支持
  • 鸭子类型,其中没有通用接口
  • DLR 互操作(请参阅注释)

但它绝对不能解决所有反射问题。

Dynamic will go a long way to solving problems with methods known only by name, where that name is known and fixed at compile time - but of course, such methods could also be expressed as interfaces if you control the types.

There are cases where dynamic would not help at all:

  • where the method name isn't known at compile time (i.e. it is loaded from config / user input)
  • object creation
  • maybe some generics scenarios

The biggest uses I see for dynamic are:

  • COM interop (obviously)
  • generic operator support
  • duck typing where there is no common interface
  • DLR interop (see comments)

But it definitely doesn't solve every reflection woe.

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