且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将动态添加到IList< T>失败了

更新时间:2023-02-17 18:46:40

在查找绑定调用的方法时,编译器(静态或动态)可能必须检查基本类型.

In looking up methods to bind a call a compiler (whether static or dynamic) may have to check on base types.

通常,在使用 dynamic 的情况下,我们要处理对 dynamic 对象本身的方法的调用,因此动态编译器从对象的具体类型开始通过 dynamic 使用,并且只需要向上继承链,直到到达 object 来寻找它们.

Generally with uses of dynamic we're dealing with a calls to methods on the dynamic object itself, so the dynamic compiler starts with the concrete type of the object being used via dynamic and need only go up its inheritance chain until it reaches object to look for them.

在将 dynamic 对象作为参数传递给方法调用的情况下,该方法调用对静态引用为接口类型的对象进行了调用,动态绑定器可悲地做同样的事情:如果没有找到所讨论类型的方法,然后查看该类型的 BaseType ,对于接口,该类型为 null .因此,它的行为与如果正确检查了继承但未能找到有问题的方法并针对该情况引发适当的异常的行为相同.(请注意,这也意味着它将找不到在 object 上定义的 Equals 方法).

In the case where a dynamic object is passed as an argument to a method call on something statically referenced as an interface type though, the dynamic binder sadly does the same thing: if it doesn't find the method on the type in question it then looks at the BaseType of that type, which for an interface is null. So it behaves the same as it would if it had checked the inheritance correctly and failed to find the method in question and throws the appropriate exception for that case. (Note that this also means it would fail to find the Equals method defined on object).

在这种情况下应该要做的是检查所讨论的接口类型所实现的所有接口.

What it should do for such cases is to check all of the interfaces implemented by the interface type in question.

此错误是 .NET Core中已修复的,但尚未移植此修复程序.NET Framework.如果要针对netfx提交错误报告,则可能要在corefx中引用该修补程序.

This bug is fixed in .NET Core but the fix has not been ported to .NET Framework. If you want to file a bug report against netfx you might want to reference that fix in corefx.

有时可以通过定义了所使用方法的基本接口访问接口类型本身,或者将其本身称为动态动态代码来解决此问题(因此,无论具体采用哪种方式,都可以找到适当的方法)类型实现).

It is sometimes possible to workaround this problem by accessing the interface type itself through the base interface where the method used is defined, or as itself being dynamic (so the appropriate method is found in whichever concreted type implements it).