是否可以知道 System.Object 是否实际上被指定为动态 (C#)?
我有几个场景涵盖了这一点,但我将专门写一个更容易证明
我有一个工厂接口的场景:
interface IFactory
{
Create<T>();
}
以及一段使用它的代码:
public static void func(IFactory f)
{
var o = f.Create<dynamic>();
}
现在,在 Create
的实现中T>()
- 有没有办法通过 T
反射来确定调用者是否打算在其对象上进行动态分派,而不是直接的对象?我有一个类,能够做出这种区分将非常有用......
我已经查看了是否可以找到类型的属性或类似的东西,但没有乐趣。
I have a couple of scenarios that cover this, but I'll write specifically about the one that's easier to demonstrate
I have a factory interface:
interface IFactory
{
Create<T>();
}
And a piece of code that uses it thus:
public static void func(IFactory f)
{
var o = f.Create<dynamic>();
}
Now, in an implementation of Create<T>()
- is is there any way, via reflection over T
, to determine if the caller is intending dynamic dispatch on their object as opposed to a straightforward object
? I have a class where being able to make that distinction would be quite useful...
I've had a look to see if I can find attributes on the type or something like that, but no joy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
动态
仅在调用者眼中,所以不会;据我所知这是不可能的。就Create
而言,它只是object
。dynamic
is in the eye of the caller only, so no; that isn't possible AFAIK. As far asCreate
is concerned it is justobject
.