简体   繁体   English

CLR如何解决涉及动态类型的方法?

[英]How does CLR resolve method involving a dynamic type?

Given, 鉴于,

Method overloading = compile time resolution. 方法重载=编译时间分辨率。

Method overriding = run-time resolution. 方法覆盖=运行时分辨率。

How does CLR resolve the following method call (involving a dynamic type) ? CLR如何解决以下方法调用(涉及动态类型)?

    dynamic intValue = -10;
    var result = Math.Abs(intValue);

Thanks for your interest. 谢谢你的关注。

The process of connecting names to what they are is called binding . 将名称与它们的名称相关联的过程称为绑定 Normal overload resolution is 'early' binding, because the exact meaning of the method name is determined early - at compile time. 正常的重载解析是'早期'绑定,因为方法名称的确切含义是在编译时提前确定的。

When the compiler encounters a virtual method during overload resolution, it emits a virtual call. 当编译器在重载解析期间遇到虚方法时,它会发出虚拟调用。 At runtime, this is routed to the correct method, possibly to an override. 在运行时,它被路由到正确的方法,可能是覆盖。

When the compiler encounters a dynamic object, it emits code to perform 'late' binding, ie at runtime. 当编译器遇到动态对象时,它会发出代码来执行'后期'绑定,即在运行时。 Late binding is like overload resolution at runtime. 后期绑定就像运行时的​​重载解析一样。 The code looks at the argument, finds out its an integer, looks up the correct overload to call, and calls it. 代码查看参数,找出它的整数,查找正确的调用重载并调用它。

Well, it actually does a bit more than that. 嗯,它确实比那更多。 It caches the result of the lookup and puts in a test so that the next time the code is run, it can go right to calling the correct method if the argument type is int . 它缓存查找的结果并进行测试,以便下次运行代码时,如果参数类型为int ,则可以正确调用正确的方法。

This is still a simplification. 这仍然是一种简化。 In reality, several more optimizations may be done to gain optimal performance. 实际上,可以进行几个更优化以获得最佳性能。

See this blog post for a more thorough explanation of exactly what would happen with your example. 请参阅此博客文章 ,以更全面地了解您的示例会发生什么。

Method binding that involves dynamic type parameters is a bit tricky in C#. 涉及动态类型参数的方法绑定在C#中有点棘手。 The first and basic rule the compiler uses is: Types take precedence over dynamic. 编译器使用的第一个基本规则是:类型优先于动态。 for example: 例如:

void method(dynamic x, string y)
    {
        //do something 
    }
void method(int x, string y) { // do something }
a call to the method with arguments int and string method(5,"some string) would be resolved to the second function. However if the parameters of either of the methods were both dynamic the compiler would complain about ambiguous method call. 调用带有参数int的方法和字符串method(5,"some string)将解析为第二个函数。但是如果任一方法的参数都是动态的,编译器会抱怨模糊的方法调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET 4.0中新的“动态”变量类型是否解决了CLR中的单/多方法调度问题? - Does new 'dynamic' variable type in .NET 4.0 solve the single/multiple method dispatch issue in CLR? CLR如何知道盒装对象的类型? - How does the CLR know the type of a boxed object? 当调用静态方法时,CLR如何管理? - How does CLR manage when a static method is called? 究竟什么是CLR参考,它如何保存类型信息? - What exactly is a CLR reference, and how does it hold type information? 如何解决实体框架无法在涉及导航属性的查询中创建类型的常量值? - How to resolve Entity Framework Unable to create a constant value of type in a query involving navigation properties? 如何用动态参数解析泛型类型? - How to resolve generic type with dynamic parameter? IDataServiceMetadataProvider / ResourceType ...对于没有CLR类型的动态类型该怎么办? - IDataServiceMetadataProvider / ResourceType… what for dynamic types with no CLR type? CLR是在每次方法调用之前在堆上创建Type对象,还是仅使用已经创建的Type对象? - Does the CLR create Type object on the heap right before each method invocation or does it just use already created Type object? 如何解决委托类型的方法绑定问题? - How to resolve the Method binding issue with that of delegate type? CLR如何更改用重载构造函数实例化的动态数组的容量? - How does CLR change the capacity of dynamic arrays which instantiated with overloaded constructors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM