这是动态的错误吗?
当在泛型类上使用 dynamic
实现动态分派时,并且泛型类型参数是另一个类上的私有内部类,运行时绑定器会引发异常。
例如:
using System;
public abstract class Dispatcher<T> {
public T Call(object foo) { return CallDispatch((dynamic)foo); }
protected abstract T CallDispatch(int foo);
protected abstract T CallDispatch(string foo);
}
public class Program {
public static void Main() {
TypeFinder d = new TypeFinder();
Console.WriteLine(d.Call(0));
Console.WriteLine(d.Call(""));
}
private class TypeFinder : Dispatcher<CallType> {
protected override CallType CallDispatch(int foo) {
return CallType.Int;
}
protected override CallType CallDispatch(string foo) {
return CallType.String;
}
}
private enum CallType { Int, String }
}
这里,将抛出一个 RuntimeBinderException
消息
“Dispatcher.CallDispatch(int)”由于其保护级别而无法访问
无法访问的原因是类型参数 T
是私有 CallType
,其中 Dispatcher
T>
无法访问。因此,CallDispatch
一定是不可访问的 - 但事实并非如此,因为它可以作为 T
访问。
这是 dynamic
的错误,还是不应该支持?
When implementing dynamic dispatch using dynamic
on a generic class, and the generic type parameter is a private inner class on another class, the runtime binder throws an exception.
For example:
using System;
public abstract class Dispatcher<T> {
public T Call(object foo) { return CallDispatch((dynamic)foo); }
protected abstract T CallDispatch(int foo);
protected abstract T CallDispatch(string foo);
}
public class Program {
public static void Main() {
TypeFinder d = new TypeFinder();
Console.WriteLine(d.Call(0));
Console.WriteLine(d.Call(""));
}
private class TypeFinder : Dispatcher<CallType> {
protected override CallType CallDispatch(int foo) {
return CallType.Int;
}
protected override CallType CallDispatch(string foo) {
return CallType.String;
}
}
private enum CallType { Int, String }
}
Here, a RuntimeBinderException
will be thrown with the message
'Dispatcher.CallDispatch(int)' is inaccessible due to its protection level
The reason for the inaccessibility is that the type parameter T
is the private CallType
which Dispatcher<T>
cannot access. Therefore, CallDispatch
must be inaccessible - but it isn't, because it's accessible as T
.
Is this a bug with dynamic
, or is this not supposed to be supported?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个错误。如果您可以静态地进行调用(并且您可以),那么您应该能够动态地进行调用。
具体来说,以下代码有效:
请注意,我已使用
ToString()
来使静态类型已知,C# 编译器和 CLR 允许此上下文访问私有类型CallType
>,所以 DLR 也应该允许它。It's a bug. If you can make the call statically (and you can), you should be able to make it dynamically.
Specifically, the following code works:
Note that I've used
ToString()
to make the static type known, the C# compiler and CLR allow this context to access the private typeCallType
, so the DLR should allow it as well.这是一个错误,因为以下静态类型更改应该是等效的
并且它可以工作。
这个问题似乎是编译器和它发出的 dlr 调用以及编译器在调用中包含的静态信息的问题。可以通过手动设置 dlr 调用的开源框架 ImpromptuInterface 来解决这个问题。使用 Impromptu,通过将上下文设置为
this
,它可以从运行时类型(即 TypeFinder)获取访问权限。It's a bug because the following static typing change should be equivalent
And it works.
This issue seems to be an issue with the compiler and the dlr calls it makes and the static information the compiler includes in the invocation. It can be worked around with the open source framework ImpromptuInterface that manually setups the dlr calls. With Impromptu by setting the context to
this
it's getting access permissions from the runtime type which will be TypeFinder.